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