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




ExamGuru IT Solutions - .Net Guru is owned and operated by ExamGuru, Inc., the man behind .Net Guru. If you're in the market for bespoke software or software consultancy, why not get him and his highly trained team to help? - www.examguru.net/ITCertification
Ad


Need Dot Net Interview Questions?
Ask ExamGuru, Inc. for advice and help on Passing .Net Interviews
.Net Projects
Best-of-breed application framework for .NET projects, developed by ExamGuru, Inc. and ExamGuru IT
Free .net Help
Commission ExamGuru, Inc. and his team for your next bespoke software project
FogBUGZ
The only bug tracking system carefully crafted with one goal in mind: helping teams create great software.
Awesome Tools
If you don't know about these, you're missing out... IT Certification Questions
IT Interview Questions
Free Oracle 10g Training
MCSE Boortcamp
Cisco Study Guides
Cheap Study Guides
Exact Questions
Dot Net Interview Questions
Oracle OCP
Cheap Travel
Designer Perfumes - Wholesale Prices
Free Programming Tutorials
 
ExamGuru IT Solutions - .Net Guru is owned and operated by ExamGuru, Inc., the man behind .Net Guru. If you're in the market for bespoke software or software consultancy, why not get him and his highly trained team to help? - www.examguru.net/ITCertification
 Copyright © ExamGuru, Inc. 2001-2006
Contact Us - Terms of Use - Privacy Policy - www.dot-net-guru.com - www.examguru.net - www.oraclesource.net - www.itinterviews.net - www.examguru.net/ITCertification