0

In my code (C#) I'm using a WebRequest:

var request = WebRequest.Create(url);

For some reason this line takes about 2-3 seconds to run. I've looked for a solution, but couldn't find any for the creation of the request. I've tried different urls (http and https, google, etc.) but nothing seems to help. Did anyone else experience such a behavior? Can anyone explain what exactly is going on during the creation of the request? Any alternatives?

BTW - I'm on a 64bit Win 7 (Bootcamp)

Edit - measured 3.126 seconds with Stopwatch for the following code (wireless):

var request = WebRequest.Create("http://www.google.com");

On the same network, only wired, it took 0.01 seconds.

Thanks everyone!

Barracuda
  • 101
  • 6
  • 1
    How are you measuring the amount of time it takes to create the request? – Justin Niessner Oct 25 '12 at 22:22
  • 1
    How long does it take if you just type the url into a browser? – jac Oct 25 '12 at 22:27
  • If all other sources fail, and you want to see what .Net is doing behind the scenes, you can get a copy of the .Net source itself at http://referencesource.microsoft.com/netframework.aspx. – Zugbo Oct 25 '12 at 22:31
  • Justin - I'm measuring with logging - I know it's not very accurate, but it gives an idea. Beaner - www.google.com for example takes less than a second. So while creating a request I'm actually accessing it? – Barracuda Oct 25 '12 at 22:32
  • This makes very little sense, it just creates an HttpWebRequest or FtpWebRequest instance, depending on the url. Takes 3 microseconds on my machine. You'll need to use a Stopwatch to measure this. – Hans Passant Oct 25 '12 at 22:32
  • measured with Stopwatch: 3.126 seconds – Barracuda Oct 25 '12 at 22:38
  • Perhaps your internet connection is very poor? As Beaner asked, how long does it take you to go to other pages on the internet in your browser? – Kirk Woll Oct 25 '12 at 22:43
  • As I said - http://www.google.com takes less than a second with chrome and explorer, but WebRequest.Create("http://www.google.com") takes 3.126 seconds. – Barracuda Oct 25 '12 at 22:49
  • That's very weird - on the same network, in wireless connection it took 3.126 seconds, and with wired connection - as expected 0.01 seconds. Thanks everyone! – Barracuda Oct 25 '12 at 22:54
  • When you create a `WebRequest`, it initializes the default proxy. I've had issues in the past where misconfigured proxy settings can cause a significant delay in creating `WebRequest`. – moribvndvs Oct 25 '12 at 23:15

1 Answers1

1

Some people have found that proxy detection can take a ridiculously long time. If you know there won't be a proxy, try setting the WebRequest's proxy property to null.

var request = WebRequest.Create("http://www.google.com");
request.Proxy = null;

Update

I don't have any other ideas, but here are some things you might try to narrow down the problem.

  1. Enable logging by putting the following code in your config file:

    <configuration>
      <system.diagnostics>
        <trace autoflush="true" />
        <switches>
          <add name="System.Net" value="Verbose"/>
        </switches>
      </system.diagnostics>
    </configuration>
    
  2. Check out the source code for WebRequest.Create in ILSpy or a similar tool, and try doing the same things it does directly. For example, you can try creating a new HttpWebRequest(...) rather than calling WebRequest.Create and see if you get the same behavior.

Community
  • 1
  • 1
StriplingWarrior
  • 151,543
  • 27
  • 246
  • 315
  • 1
    This has definitely been a problem for me in the past, with misconfigured proxy settings. – moribvndvs Oct 25 '12 at 23:16
  • 1
    indeed it's true, but my delay was for the first line (creation), not for the response. thanks anyways – Barracuda Oct 25 '12 at 23:19
  • @Barracuda: Ah, I was assuming it was the actual connection that was taking so long. I added a couple of additional thoughts. Good luck with this. – StriplingWarrior Oct 26 '12 at 15:41
  • @Barracuda: You may also want to check whether this issue is related: http://social.msdn.microsoft.com/Forums/en-US/netfxnetcom/thread/c0d5eb13-657d-489d-b9e4-d279d6463e8b/. It sounds like there can be some kind of deadlock when the system tries to open the config file to figure out the connection settings. – StriplingWarrior Oct 26 '12 at 15:54