WebRequest request = WebRequest.Create(someUrl); using(WebResponse response = request.GetResponse()) { using(StreamReader reader = new StreamReader(response.GetResponseStream())) { string result = reader.ReadToEnd(); } }
Unfortunately, StreamReader is only good for reading text. When it comes to binary data the result has a good chance of being incomplete. The approach for binary data is to stick to the basic Stream type and read raw bytes.
byte[] result; byte[] buffer = new byte[4096]; WebRequest wr = WebRequest.Create(someUrl); using(WebResponse response = wr.GetResponse()) { using(Stream responseStream = response.GetResponseStream()) { using(MemoryStream memoryStream = new MemoryStream()) { int count = 0; do { count = responseStream.Read(buffer, 0, buffer.Length); memoryStream.Write(buffer, 0, count); } while(count != 0); result = memoryStream.ToArray(); } } }
P.S. IDisposable lurks everywhere!. It’s a shame some classes use an explicit interface implementation and hide the Dispose method from Intellisense.
P.P.S. Commercials are the best things going on Monday Night Football these days. Except the commercials for other ABC shows. I don't know why I turn on television.
Comments
<br>
<br>In Java I have to close the connections in a try finally block. Do you need to also close the C# ones or do they close automatically when they pass from scope?
<br>
<br>Additional information: Non-negative number required.
<br>
<br>What am I doing worng
<br>
<br>It's hard to tell without seeing some code. Feel free to send me an email and I can take a look.
<br> //Joe Code
<br> ASCIIEncoding encodedData=new ASCIIEncoding();
<br> byte[] byteArray = encodedData.GetBytes(postData);
<br> Stream stream;
<br>
<br> byte[] result;
<br> byte[] buffer = new byte[4096];
<br>
<br> HttpWebRequest wr = (HttpWebRequest)WebRequest.Create(url);
<br> wr.Method = "POST";
<br> wr.ContentType = "application/x-www-form-urlencoded";
<br> wr.ContentLength = byteArray.Length;
<br>
<br> stream = wr.GetRequestStream();
<br> stream.Write(byteArray, 0, byteArray.Length);
<br>
<br> stream.Flush();
<br> stream.Close();
<br>
<br> using(WebResponse response = wr.GetResponse())
<br> {
<br> Console.WriteLine("Headers: " + response.Headers.ToString());
<br>
<br> using(Stream responseStream = response.GetResponseStream())
<br> {
<br> using(MemoryStream memoryStream = new MemoryStream())
<br> {
<br> int count = 0;
<br> do
<br> {
<br> count = responseStream.Read(buffer, 0, buffer.Length);
<br> memoryStream.Write(buffer, 0, count);
<br>
<br> } while(count != 0);
<br>
<br> result = memoryStream.ToArray();
<br> }
<br> }
<br> }
<br>
<br> //Console.ReadLine();
<br> }
<br>
<br>while(count != 0);
<br>
<br>What's odd is I know I have a response, but for some reason I just can't open that attachment and read it to save it onto my server.
<br>
<br>count = responseStream.Read(buffer, 0, buffer.Length);
<br>memoryStream.Write(buffer, 0, count);
<br>
<br>Read() returns -1 when it has no more data. Trying to subsequently do a Write() with count of -1 most likely caused the problem.
<br>
<br>/Swee
Any Ideas would be appreciated!
Marc
Lampcov@aim.com
<%@ Import Namespace="System.Net" %>
<%@ Import Namespace="System.IO" %>
<script language="c#" runat="server" debug="true">
private void Page_Load(object sender, System.EventArgs e)
{
byte[] result;
byte[] buffer = new byte[4096];
WebRequest wr = WebRequest.Create("localhost:16200/?MRN=123&doc_type=pdf");
WebResponse response = wr.GetResponse();
Stream responseStream = response.GetResponseStream();
MemoryStream memoryStream = new MemoryStream();
Response.AddHeader("content-disposition", "filename=report.PDF");
Response.ContentType = "application/pdf";
int count = 0;
do
{
count = responseStream.Read(buffer, 0, buffer.Length);
memoryStream.Write(buffer, 0, count);
Response.BinaryWrite(buffer);
//Response.OutputStream.Write(buffer,0,count);
}
while (count != 0);
result = memoryStream.ToArray();
}
</script>