Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / New Users / September 2004

Tip: Looking for answers? Try searching our database.

Reading binary files through an WebResponse...

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Carlo Razzeto - 02 Sep 2004 03:04 GMT
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
Carlo Razzeto - 02 Sep 2004 03:19 GMT
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
Jon Skeet [C# MVP] - 02 Sep 2004 07:30 GMT
> 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.

Signature

Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Carlo Razzeto - 07 Sep 2004 03:42 GMT
> 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
Jon Skeet [C# MVP] - 02 Sep 2004 07:30 GMT
> 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
[quoted text clipped - 6 lines]
> string strSrcURI = this.strRootURI + '/' + this.UserName + '/' +
> this.MailBox + '/' + this.messageName + '/' + this.attachments[i];

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

Signature

Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Carlo Razzeto - 07 Sep 2004 03:41 GMT
<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.

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 [C# MVP] - 07 Sep 2004 06:46 GMT
> > 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?
[quoted text clipped - 9 lines]
> 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.

Signature

Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.