stiopa
stiopa

Reputation: 137

Speed of HttpWebRequest/HttpWebResponse

Is there any quicker alternative to the code below to get a http response into a string?

string req = "http://someaddress.com";
Stopwatch timer = new Stopwatch();
timer.Start();
using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
{
      using (Stream dataStream = response.GetResponseStream())
      {
            StreamReader reader = new StreamReader(dataStream);
            reader.ReadToEnd();
      }
}
timer.Stop();
Console.WriteLine(timer.Elapsed);

The response is pretty big - around 2MB and is in XML format. Affter this codes completes, the timer is equal to ~50 seconds. When I paste the same url into the browser window it takes about 35 seconds for it to display the xml document.

Upvotes: 3

Views: 2174

Answers (1)

Jon Skeet
Jon Skeet

Reputation: 1499790

(You should have a using statement for the response, by the way... and I agree with asbjornu's comment. You should update your question with more details.)

You should use something like Wireshark to look at what the requests and responses look like in each case. For example, is the browser specifying that it supports compressed responses, and WebRequest not? If it's over a slow connection, that could well be the important part.

Another thing to test is whether the string decoding is taking significant time in the .NET code... if you simply read the data from the stream into a byte array (possibly just throwing it away as you read it) is that significantly faster? For example:

using (var response = request.GetResponse())
{
    using (var stream = response.GetResponseStream())
    {
        // Just read the data and throw it away
        byte[] buffer = new byte[16 * 1024];
        int bytesRead;
        while ((bytesRead = stream.Read(buffer, 0, buffer.Length)) > 0)
        {
            // Ignore the actual data
        }
    }
}

Upvotes: 3

Related Questions