28

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 efficient way to read an http response.

I need the output as string, I've seen an article 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?

Kevin Panko
  • 8,356
  • 19
  • 50
  • 61

6 Answers6

19

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
  • 38,975
  • 25
  • 98
  • 152
  • For transferring binary data (like pictures), would it be inefficient to use StreamReader/string and convert to a byte array after? Want to avoid dealing with buffer resizing when reading from a Stream (vs StreamReader)? Would this be bad coding practice, even for small transfers (< 1Mb). What would be best response encoding for binary data (would ASCII be most efficient to convert to a byte array, vs say UTF-8)? Thanks. – samus Oct 06 '16 at 19:01
  • **Answer:** http://stackoverflow.com/questions/5867227/convert-streamreader-to-byte – samus Oct 06 '16 at 19:05
18

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))
            {
                var buffer = new byte[4096];
                long totalBytesRead = 0;
                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...
    // Remove this catch if you don't want to handle errors here.
    throw;
}
usr
  • 168,620
  • 35
  • 240
  • 369
Mitch Wheat
  • 295,962
  • 43
  • 465
  • 541
14

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 Rînea
  • 20,288
  • 17
  • 117
  • 166
7

In powershell, I have this function:

function GetWebPage
{param ($Url, $Outfile)
    $request = [System.Net.HttpWebRequest]::Create($SearchBoxBuilderURL)
    $request.AuthenticationLevel = "None"
    $request.TimeOut = 600000     #10 mins 
    $response = $request.GetResponse() #Appending "|Out-Host" anulls the variable
    Write-Host "Response Status Code: "$response.StatusCode
    Write-Host "Response Status Description: "$response.StatusDescription
    $requestStream = $response.GetResponseStream()
    $readStream = new-object System.IO.StreamReader $requestStream
    new-variable db | Out-Host
    $db = $readStream.ReadToEnd()
    $readStream.Close()
    $response.Close()
    #Create a new file and write the web output to a file
    $sw = new-object system.IO.StreamWriter($Outfile)
    $sw.writeline($db) | Out-Host
    $sw.close() | Out-Host
}

And I call it like this:

$SearchBoxBuilderURL = $SiteUrl + "nin_searchbox/DailySearchBoxBuilder.asp"
$SearchBoxBuilderOutput="D:\ecom\tmp\ss2.txt"
GetWebPage $SearchBoxBuilderURL $SearchBoxBuilderOutput
Stew-au
  • 434
  • 5
  • 13
3

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

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

    while ((bytesRead = .... 
1

I faced a similar situation:

I was trying to read raw response in case of an HTTP error consuming a SOAP service, using BasicHTTPBinding.

However, when reading the response using GetResponseStream(), got the error:

Stream not readable

So, this code worked for me:

try
{
    response = basicHTTPBindingClient.CallOperation(request);
}
catch (ProtocolException exception)
{
    var webException = exception.InnerException as WebException;

    var alreadyClosedStream = webException.Response.GetResponseStream() as MemoryStream;
    using (var brandNewStream = new MemoryStream(alreadyClosedStream.ToArray()))
    using (var reader = new StreamReader(brandNewStream))
        rawResponse = reader.ReadToEnd();
}
João Paulo Melo
  • 141
  • 1
  • 3
  • 9
  • 1
    Sure, using a MemoryStream in conjunction with StreamReader.ReadToEnd() just to decode the UTF8 string from alreadyClosedStream.ToArray() works. But so does `rawResponse = System.Text.Encoding.UTF8.GetString(alreadyClosedStream.ToArray())`, and it is way more simple and easier to read... ;) –  Feb 14 '18 at 11:52