views:

3708

answers:

4

What is the best way to read an HTTP response from GetResponseStream ?

Currently I'm using the following approach.

Using SReader As StreamReader = New StreamReader(HttpRes.GetResponseStream)
   SourceCode = SReader.ReadToEnd()
End Using

I'm not quite sure if this is the most effecient way to read an http response.

I need the output as string, I've seen an article ( http://www.informit.com/guides/content.aspx?g=dotnet&seqNum=583 ) with a different approach but I'm not quite if it's a good one. And in my tests that code had some encoding issues with in different websites.

How do you read web responses?

+2  A: 

I use something like this to download a file from a URL:

   if (!Directory.Exists(localFolder))
   {
        Directory.CreateDirectory(localFolder);   
   }


    try
    {
        HttpWebRequest httpRequest = (HttpWebRequest)WebRequest.Create(Path.Combine(uri, filename));
        httpRequest.Method = "GET";

        // if the URI doesn't exist, an exception will be thrown here...
        using (HttpWebResponse httpResponse = (HttpWebResponse)httpRequest.GetResponse())
        {
            using (Stream responseStream = httpResponse.GetResponseStream())
            {
                using (FileStream localFileStream = 
                  new FileStream(Path.Combine(localFolder, filename), FileMode.Create))
                {
                    int bytesRead;
                    while ((bytesRead = responseStream.Read(buffer, 0, buffer.Length)) > 0)
                    {
                        totalBytesRead += bytesRead;
                        localFileStream.Write(buffer, 0, bytesRead);
                    }
                }
            }
        }
    }
    catch (Exception ex)
    {
        // You might want to handle some specific errors : Just pass on up for now...
        throw;
    }
Mitch Wheat
A: 

You forgot to define "buffer" and "totalBytesRead":

using ( FileStream localFileStream = ....  
{  
    byte[] buffer = new byte[ 255 ];  
    int bytesRead;  
    double totalBytesRead = 0;  

    while ((bytesRead = ....
it was a code snippet I pulled out of existing code...
Mitch Wheat
+1  A: 

Maybe you could look into the WebClient class. Here is an example :

using System.Net;

namespace WebClientExample
{
    class Program
    {
        static void Main(string[] args)
        {
            var remoteUri = "http://www.contoso.com/library/homepage/images/";
            var fileName = "ms-banner.gif";
            WebClient myWebClient = new WebClient();
            myWebClient.DownloadFile(remoteUri + fileName, fileName);
        }
    }
}
Andrei Rinea
A: 

My simple way of doing it to a string. Note the true second parameter on the StreamReader constructor. This tells it to detect the encoding from the byte order marks and may help with the encoding issue you are getting as well.

string target = string.Empty;
HttpWebRequest httpWebRequest = (HttpWebRequest)WebRequest.Create("http://www.informit.com/guides/content.aspx?g=dotnet&seqNum=583");

HttpWebResponse response = (HttpWebResponse)httpWebRequest.GetResponse();
try
{
  StreamReader streamReader = new StreamReader(response.GetResponseStream(),true);                
  try
  {
    target = streamReader.ReadToEnd();
  }
  finally
  {
    streamReader.Close();
  }
}
finally
{
  response.Close();
}
Robert MacLean