BlockLeftTop, PRELOAD BlockLeftBottom, PRELOAD BlockLeftStretch, PRELOAD BlockTop, PRELOAD BlockBottom, PRELOAD BlockStretch, PRELOAD BlockRightTop, PRELOAD BlockRightBottom, PRELOAD BlockRightStretch, PRELOAD
DeltaEngine

HttpWebRequest is very slow and taking forever on Windows 7

by Benjamin Nitschke 7. April 2009 17:23
Update 2009-08-06: This issue was fixed in Windows 7 RC and Windows 7 RTM :)

I was going nuts yesterday night after I wrote a couple of unit tests that involved the C# HttpWebRequest class. For some strange reason it always takes 20-30 seconds to create a HttpWebRequest with an url and calling the GetResponse method on Windows 7. I was thinking maybe the server I'm contacting is kinda slow, but it was really fast in the browser. So maybe my SSD drive is messed up? Ok, tried on a normal HDD, same thing. Okay, then maybe TestDriven.net is doing something strange, lets write a simple console application. Wtf, still the same issue! Then I tried it on a different Windows 7 PC, same issue.

I gave up (was very late anyway) and went to work to test it again in the morning on my work PC (also Windows 7). Still the same freaking issue, it takes forever to get anything connected with HttpWebRequest and HttpWebResponse. Getting the actual data is like 30 times faster (and should not matter anyway), still slower than I remembered, but the issue was just in the following two lines (first line took 2-5s, second line 20-25s):

    HttpWebRequest httpRequest = (HttpWebRequest)WebRequest.Create(url);
    HttpWebResponse httpResponse = (HttpWebResponse)httpRequest.GetResponse();

Maybe this isn't the right comic, but its always good to make fun of not-perfectly-working operating systems:




Back to the Problem: I rewrote my console test application again (from last night) and compiled it on my work PC:

using System;
using System.Net;

namespace TestHttpRequest
{
    class Program
    {
        static void Main(string[] args)
        {
            //WTF is going on here? Takes 30s on Windows 7, <100ms on Windows Vista
            string url = "http://www.google.com";

            Console.WriteLine("Connecting: " + url);
            HttpWebRequest httpRequest = (HttpWebRequest)WebRequest.Create(url);
            HttpWebResponse httpResponse = (HttpWebResponse)httpRequest.GetResponse();

            Console.WriteLine("Connected ..");
            if (httpResponse.StatusCode == HttpStatusCode.OK)
            {
                // Dummy
                Console.WriteLine("Getting data ..");
                httpResponse.GetResponseStream();
            } // if
            else
                Console.WriteLine("Failed to get any data ..");

            Console.WriteLine("Done ..");
        }
    }
}


Guess what, still the same issue, takes around 30 seconds to do this very simple job in Windows 7. Could not really figure out the real cause of this (I'm not really that interested, I just wanted to write a couple of unit tests to make some things easier), but it seems like the Conhost.exe file (Console Window Host) of Windows 7 is doing a lot while this is going on (I even have different Windows 7 versions installed, it is the same issue on all of them). Could not see any networking going on (maybe 0.01 kb every 5 seconds) either. If I completely disable networking the GetResponse immediately crashes (it does not take 2-3s for the WebRequest.Create to do its work, and the exception occurs instantly), but that is not really helpful because I wanted to test the response ..

Instead of guessing what this could be about I just wanted to make sure this is a problem with my Windows 7, so I gave my test console application to my colleague that is running Windows Vista and the thing ran in about 20ms, there is nothing wrong with the code, it runs perfectly fine on a non-Windows 7 machine!

So I tried the same stuff in different ways, like using the WebClient class, same result, still 30 seconds for the request.

            // Same stuff with webclient
            WebClient webClient = new WebClient();
            Console.WriteLine("Downloading: " + url);
            byte[] data = webClient.DownloadData(url);
            Console.WriteLine("Done ..");
            
Next I implemented the same stuff with sockets (more specifically TcpClient, which is based on Sockets, but a little easier to use), which is kinda ugly and more work, but at least I finally got the response time I was looking for (<100ms). Microsoft should maybe look into this Windows 7 issue .. for my unit tests I can now use my own HttpWebResponse class and all problems are gone :)

            // Another try, this time with TcpClient ..
TcpClient client = new TcpClient("www.google.com", 80);

// Get the stream from the tcp client (allows us to read and write)
NetworkStream stream = client.GetStream();

// Write the http request ourselfs (Note: Needs 2 empty lines at the end)
byte[] requestBytes = Encoding.ASCII.GetBytes(
@"GET / HTTP/1.1
Host: www.google.com
Connection: Close

"
);
// Send this ound
stream.Write(requestBytes, 0, requestBytes.Length);
stream.Flush();

// We can't use DataAvailable here because we don't know when the data
// will arrive. So just call ReadByte until it returns -1 and then
// we know we are done (Note: This will block our application,
// if you really care about performance write it asyncronly).
Console.WriteLine("Getting data ..");
string result = "";
int aByte = 0;
do
{
aByte = stream.ReadByte();
if (aByte >= 0)
result += (char)aByte;
} while (aByte >= 0);
Console.WriteLine("Result=" + result);
client.Close();

Comments


4/8/2009 1:48:31 AM #

Time to fill a report in connect! :p It would be nice to know why W7 is doing such a weird thing honestly...

Vicente | Reply



4/8/2009 3:56:33 AM #

Running the sample app (spiced with some time display) above on Vista and W7 in a VM results in:

Vista:
Connecting: http://www.google.com" rel="nofollow">http://www.google.com (0ms)
Connected (300ms)..
Getting data (300ms)..
Done in 300ms.

W7:
Connecting: http://www.google.com" rel="nofollow">http://www.google.com (0ms)
Connected (2926ms)..
Getting data (2927ms)..
Done in 2927ms.

While W7 is still significantly slower (which might be due to it running in a VM) your problems could be caused by some weird network issue instead of the beta OS.

Björn Graf | Reply



4/18/2010 5:42:15 PM #

Nice post thanks.

us | Reply



4/28/2010 6:20:22 AM #

I\'m happy I found this blog, I couldnt discover any info on this subject matter prior to. I also run a site and if you want to ever serious in a little bit of guest writing for me if possible feel free to let me know, i\'m always look for people to check out my site. Please stop by and leave a comment sometime!

Rapidshare | Reply



5/4/2010 10:17:43 PM #

Update 2009-08-06: This issue was fixed in Windows 7 RC and Windows 7 RTM SmileI was going nuts yesterday night after I wrote a couple of unit tests that involved the C# HttpWebRequest class.  For some strange reason it always takes 20-30 seconds to create a HttpWebRequest with an url and calling the GetResponse method on Windows 7.  I was thinking maybe the server I'm contacting is kinda slow, but it was really fast in the browser.

Rapidshare Search engine | Reply



5/11/2010 8:14:55 AM #

This post is about the writer’s bad experience with the Windows 7 with HttpWebRequest. He also found the solution for it. Windows 7 took almost 30 seconds for HttpWebRequest at first. He has implemented the same stuff with sockets (more specifically TcpClient, which is based on Sockets and then the time reduced and the problem become vanished. But for that some nasty work is needed. Microsoft’s update in 2009-08-06 fixed this problem in Windows 7 RC and Windows 7 RTM. So he doesn’t have to worry about it anymore. Any way it’s a really interesting experience and thanks for sharing it with us.

Business Process Automation | Reply


Add comment




biuquote
  • Comment
  • Preview
Loading



Disclaimer: The opinions expressed in this blog are own personal opinions and do not represent the companies view.
© 2000-2011 exDream GmbH & MobileBits GmbH. All rights reserved. Legal/Impressum

Poll

Which platform should Soulcraft be released on next?











Show Results Poll Archive

Recent Games

Soulcraft

Fireburst

Jobs @ exDream

Calendar

<<  May 2012  >>
MoTuWeThFrSaSu
30123456
78910111213
14151617181920
21222324252627
28293031123
45678910