WebClient.DownloadFile is taking way too long before sending the first request
I have written a very simple console application which is just downloading a file using the following piece of code:
System.Net.WebClient web = new System.Net.WebClient ();
web.DownloadFile ("http://www.google.com", "file.txt");
On my previous development machine, this was very fast. As soon as I executed the DownloadFile
method, the request went over the wire and the response came back very soon. I inspected this behaviour with the ProcMon tool.
However, after switching to another machine, which happens to be much more powerful than my initial development box, I noticed that nothing would happen for about 5-8 seconds, when calling into DownloadFile
. Going to the same URL with a browser would show almost immediate results.
After spending several hours looking into this, stepping into the .NET source code, I finally gave up in System.Net.ServicePointManager.FindServicePoint
where the stepping somehow no longer made sense to me, and where some proxy resolution seemed to be in cause.
I finally turned off Automatically detect settings in the LAN Settings dialog of the the Internet Options dialog, found in Internet Explorer 8 (I am running Windows 7 x64), leaving the dialog with no checkbox set. This magically made everything do very, very fast. No more delay.
Well, I've found a way to circumvent the problem I was observing, but I'd be glad if somebody could share some ideas as to what might be wrong with my code. Can I somehow 开发者_如何学Pythonachieve the same effect by somehow configuring the WebClient
instance?
Have you tried explicitly setting the proxy?
web.Proxy = new WebProxy();
That should basically be equivalent to saying, "Don't use a proxy at all - don't even try."
Have you tried setting the web.Proxy properties?
Ex:
System.Net.WebClient web = new WebClient();
web.Proxy = new WebProxy("Address here");
web.DownloadFile("http://www.google.com", "file.txt");
精彩评论