Scott Seely

Sponsors

The Lounge

Wicked Cool Jobs

Syndication

News

  • INETA Community Champions
Reading a WebResponse into a byte[]

This question came up on Twitter. I’m posting the solution here for posterity. How do you read a non-seekable Stream into a byte[]? Specifically, a HttpWebResponse? Like this:

 

class Program
{
  static void Main(string[] args)
  {
    var request = WebRequest.Create("http://www.scottseely.com/blog.aspx");
    var response = request.GetResponse() as HttpWebResponse;
    var stream = response.GetResponseStream();
    var buffer = new byte[int.Parse(response.Headers["Content-Length"])];
    var bytesRead = 0;
    var totalBytesRead = bytesRead;
    while(totalBytesRead < buffer.Length)
    {
      bytesRead = stream.Read(buffer, bytesRead, buffer.Length - bytesRead);
      totalBytesRead += bytesRead;
    }
    stream.Close();
    Console.WriteLine(Encoding.UTF8.GetString(buffer, 0, totalBytesRead));
  }
}


Posted 06-15-2010 3:04 PM by Scott Seely

[Advertisement]

Comments

Geri wrote re: Reading a WebResponse into a byte[]
on 06-17-2010 4:13 AM

What about using a StreamReader?

Oytun Yilmaz wrote re: Reading a WebResponse into a byte[]
on 06-19-2010 9:57 PM

For this specific purpose you may use WebClient class

msdn.microsoft.com/.../system.net.webclient(VS.80).aspx

Scott Seely wrote re: Reading a WebResponse into a byte[]
on 06-20-2010 9:14 AM

These are all good comments. The question that came up on twitter was about reading the contents into a byte array. My demo showed how to read from a text based source and then print out the results. The intent was for something that might read contents that are genuinely binary in nature, but to show the results as something that you could recognize and say "hey, that worked". If I just wanted the string, WebClient would have been my preferred method, with StreamReader.ReadToEnd() being another nice option.

SpeedSX wrote re: Reading a WebResponse into a byte[]
on 07-14-2010 10:20 AM

Why not closing response streams after usage? I got weird bugs doing that way.

Scott Seely wrote re: Reading a WebResponse into a byte[]
on 07-18-2010 8:44 PM

SpeedSX- It was just a short program but you are correct- closing the resource is better.

Add a Comment

(required)  
(optional)
(required)  
Remember Me?

About The CodeBetter.Com Blog Network
CodeBetter.Com FAQ

Our Mission

Advertisers should contact Brendan

Subscribe
Google Reader or Homepage

del.icio.us CodeBetter.com Latest Items
Add to My Yahoo!
Subscribe with Bloglines
Subscribe in NewsGator Online
Subscribe with myFeedster
Add to My AOL
Furl CodeBetter.com Latest Items
Subscribe in Rojo

Member Projects
DimeCasts.Net - Derik Whittaker

Friends of Devlicio.us
Red-Gate Tools For SQL and .NET

NDepend

SlickEdit
 
SmartInspect .NET Logging
NGEDIT: ViEmu and Codekana
LiteAccounting.Com
DevExpress
Fixx
NHibernate Profiler
Unfuddle
Balsamiq Mockups
Scrumy
JetBrains - ReSharper
Umbraco <-- NEW Friend!
NServiceBus <-- NEW Friend!

 



Site Copyright © 2007 CodeBetter.Com
Content Copyright Individual Bloggers

 

Community Server (Commercial Edition)