0

I have implemented a simple method that periodically polls a service in order to get the public IP address.

Stopwatch sw = new Stopwatch();

while (true)
{
    Console.WriteLine("(HttpWebRequest)WebRequest.Create...");

    sw.Restart();
    HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://checkip.dyndns.org/");
    sw.Stop();

    Console.WriteLine("HttpWebRequest was created in {0} ms", sw.ElapsedMilliseconds);

    request.Proxy = null;

    sw.Restart();
    using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
    {
        sw.Stop();
        Console.WriteLine("Response was received in {0} ms", sw.ElapsedMilliseconds);

        StreamReader sr = new StreamReader(response.GetResponseStream());
        string responseStr = sr.ReadToEnd().Trim();

        string ipStr = (new Regex(@"\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}")).Matches(responseStr)[0].ToString();
        IPAddress address = IPAddress.Parse(ipStr);

        Console.WriteLine("IP: {0}", address.ToString());
    }

    Console.WriteLine();
    Thread.Sleep(60000);
}

The method WebRequest.Create takes nearly 10 seconds to create the HttpWebRequest for the first time, while on subsequent calls the same instance is created almost instantaneously. What are the reasons for this behavior?

UPDATE. Here is my output when I launch the executable (release mode) from Visual Studio.

(HttpWebRequest)WebRequest.Create...
HttpWebRequest was created in 9758 ms
Response was received in 683 ms

(HttpWebRequest)WebRequest.Create...
HttpWebRequest was created in 0 ms
Response was received in 617 ms

(HttpWebRequest)WebRequest.Create...
HttpWebRequest was created in 0 ms
Response was received in 1034 ms

(HttpWebRequest)WebRequest.Create...
HttpWebRequest was created in 0 ms
Response was received in 641 ms

(HttpWebRequest)WebRequest.Create...
HttpWebRequest was created in 0 ms
Response was received in 652 ms

(HttpWebRequest)WebRequest.Create...
HttpWebRequest was created in 0 ms
Response was received in 672 ms

(HttpWebRequest)WebRequest.Create...
HttpWebRequest was created in 0 ms
Response was received in 658 ms

(HttpWebRequest)WebRequest.Create...
HttpWebRequest was created in 0 ms
Response was received in 655 ms

(HttpWebRequest)WebRequest.Create...
HttpWebRequest was created in 0 ms
Response was received in 1067 ms

(HttpWebRequest)WebRequest.Create...
HttpWebRequest was created in 0 ms
Response was received in 651 ms

(HttpWebRequest)WebRequest.Create...
HttpWebRequest was created in 0 ms
Response was received in 643 ms

Edit:
I'm using Windows 7 SP1 32-bit on a computer with 2 GB of RAM, Intel Core 2 Duo T7500, connection speed at 7 Mbps in download and 640 Kbps in upload.

Noctis
  • 11,175
  • 3
  • 40
  • 79
enzom83
  • 7,780
  • 9
  • 58
  • 112
  • 2
    http://stackoverflow.com/questions/2519655/httpwebrequest-is-extremely-slow – Habib Nov 04 '13 at 22:13
  • @Habib: I've already read the thread you indicated, however my problem is not the first call to `GetResponse` method, but the creation of the first instance of `HttpWebRequest` class. Moreover, I have already set the `Proxy` property to `null` in my code. – enzom83 Nov 04 '13 at 22:20
  • Setting the proxy to null seems to be working for me on my machine. Can you maybe post the output you get? – Styxxy Nov 04 '13 at 22:40
  • @Styxxy: I posted the output. – enzom83 Nov 04 '13 at 23:15
  • 3
    Candidate #1: your DNS server. Candidate #2: your anti-malware software. Candidate #3: your firewall. Candidate n+1: your boss spying on your Internet usage. – Hans Passant Nov 05 '13 at 00:04
  • @HansPassant: #1 depends, the moment you create the WebRequest, there still is no DNS call. I propose a candidate #4: when the WebRequest is created, it actually also sets the proxy setting, maybe there is the bottleneck. – Styxxy Nov 05 '13 at 00:28
  • @enzom83: try adding the following in your app.config: ``. This will disable the default proxy for your app (and should hopefully make the creation of the first request a lot faster). – Styxxy Nov 05 '13 at 00:29
  • I noticed that the above problem occurs only launching the application via Visual Studio. However, if I manually launch the executable file, the above problem does not occur. – enzom83 Nov 22 '13 at 12:22

0 Answers0