Reading binary files through an WebResponse...
Messages   Related Types
This message was discovered on microsoft.public.dotnet.framework.
Responses highlighted in red are from those people who are likely to be able to contribute good, authoratitive information to this discussion. They include Microsoft employees, MVP's and others who IMHO contribute well to these kinds of discussions.
Post a new message to this list...

Carlo Razzeto
Hello, I'm having trouble properly reading and decoding binary files (eg MS
Word Doc, Adobe PDF) from a stream created from a Web Response. I have an
application that downloads email attachments from an exchange server and
will then insert them into a database. My problem is while a StreamReader
will read from the produced by a web response the file that comes out the
other end is garbage. I've experimented with creating a binary reader based
on this stream and trying to read in the data as bytes but this is not
working out. Here is the code I have thus far (in breif):
string strSrcURI = this.strRootURI + '/' + this.UserName + '/' +
this.MailBox + '/' + this.messageName + '/' + this.attachments[i];

try

{

extention = this.get_extention( this.attachments[i] );

<snip authentication, this works>

Request = (System.Net.HttpWebRequest)HttpWebRequest.Create(strSrcURI);

<snip adding credentials>

Request.Method = "GET";

// Set the Translate header.

Request.Headers.Add("Translate","f");

Response = (HttpWebResponse)Request.GetResponse();

//The following lines work, I get my content type and content length
correctly

this.attachment_type[i] = Response.ContentType;

this.attachmet_size[i] = Response.ContentLength;

this.attachment_data[i] = new byte[this.attachmet_size[i]];

//This creates the stream object

ResponseStream = Response.GetResponseStream();

BinaryReader binread = new BinaryReader( ResponseStream, Encoding.UTF8 );

int index = 0;

/********************************************

Here is my problem, my while loop does not execute. Can a

binaryread simply not use the stream created from a

webresponse object?

*********************************************/

while( binread.PeekChar() != -1 )

{

this.attachment_data[i][index++] = binread.ReadByte();

}

binread.Close();

// Clean up.

Response.Close();

ResponseStream.Close();

}//Snip exception handling

Reply to this message...
 
    
Carlo Razzeto
Ok, I decided to play with fire and read from my binary reader from 0 to <
content length. I figured I was playing with fire here "blindly" reading
from the stream with out really knowing if there was usable data there....
BUT! It worked! I'm finally getting my data! So I"m guessing that peekchar
really doesn't work the way I think it does? Anyone out there know why my
previous method was failing? I"m really curiose here!

Carlo

Reply to this message...
 
    
Jon Skeet [C# MVP] (VIP)
Carlo Razzeto <Click here to reveal e-mail address> wrote:
[Original message clipped]

The problem is that PeekChar is trying to read a *character* (in this
case in the UTF-8 encoding) when the data isn't character data to start
with.

--
Jon Skeet - <Click here to reveal e-mail address>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Reply to this message...
 
    
Carlo Razzeto
"Jon Skeet [C# MVP]" <Click here to reveal e-mail address> wrote in message
news:Click here to reveal e-mail address...
[Original message clipped]

Yeah, that seems to make sence now that I think about it, thanks!

Carlo

Reply to this message...
 
    
Jon Skeet [C# MVP] (VIP)
Carlo Razzeto <Click here to reveal e-mail address> wrote:
[Original message clipped]

<snip>

I don't see why you're using a BinaryReader at all. Why not just read
from the stream until it's exhausted, as if you were reading a file?

See http://www.pobox.com/~skeet/csharp/readbinary.html for some sample
code to completely read a stream and return a byte array with the
contents.

--
Jon Skeet - <Click here to reveal e-mail address>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Reply to this message...
 
    
Carlo Razzeto
"Jon Skeet [C# MVP]" <Click here to reveal e-mail address> wrote in message
news:Click here to reveal e-mail address...
<snip>
[Original message clipped]

I wanted to ensure that I was reading that data in correctly, as Strings and
Chars in .Net seem to murder binaries, which I guess is because they are
nativly Unicode and not ASCII. I wasn't aware it was possible to do this
with the simple stream reader, I thought it just translated everything to a
native .Net string/char. Anyway, thanks for all the information, it was very
informative.

Carlo

Reply to this message...
 
    
Jon Skeet [C# MVP] (VIP)
Carlo Razzeto <Click here to reveal e-mail address> wrote:
[Original message clipped]

I wasn't suggesting using a StreamReader. I was suggesting just using
Stream.

--
Jon Skeet - <Click here to reveal e-mail address>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Reply to this message...
 
 
System.IO.BinaryReader
System.IO.StreamReader
System.Net.HttpWebRequest
System.Net.HttpWebResponse
System.Text.Encoding




Ad
MBR BootFX
Best-of-breed application framework for .NET projects, developed by Matthew Baxter-Reynolds and MBR IT
 
 Copyright © Matthew Baxter-Reynolds 2001-2008. '.NET 247 Software Development Services' is a trading style of MBR IT Solutions Ltd.
Contact Us - Terms of Use - Privacy Policy - www.dotnet247.com