Reading binary files through an WebResponse...

  • Thread starter Thread starter Carlo Razzeto
  • Start date Start date
C

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;

try

{

extention = this.get_extention( this.attachments );

<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 = Response.ContentType;

this.attachmet_size = Response.ContentLength;

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

//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[index++] = binread.ReadByte();

}

binread.Close();

// Clean up.

Response.Close();

ResponseStream.Close();

}//Snip exception handling
 
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
 
Carlo Razzeto said:
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;


<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.
 
Carlo Razzeto said:
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!

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.
 
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.

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
 
Jon Skeet said:
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.

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

Carlo
 
Carlo Razzeto said:
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.

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