Weird WebClient behavior: 1 computer hangs, others don't for same file

Encountered the same problem, but found another solution. Quite complex discussion here: http://social.msdn.microsoft.com/Forums/en-US/a00dba00-5432-450b-9904-9d343c11888d/webclient-downloadstringasync-freeze-my-ui?forum=ncl

In short, the problem is web client is searching for proxy servers and hanging the app. The following solution helps:

WebClient webClient = new WebClient();
webClient.Proxy = null;
... Do whatever else ...

After hours of banging my head on the screen, a pleasant session with WireShark and several separate sessions with Fiddler, I found the answer. Sharing it here in case others have the same problem.

It turns out that any use of HttpWebRequest prior would cause this behavior if the WebResponse object was not .Close()d properly. No matter if you create multiple WebClient or HttpWebRequest objects. They will all fail.

The clue came from the following observation: watching the TCP back-and-forth in WireShark, it became clear that nothing was being sent to the server for the second request (the one that was failing). Why did this work with Fiddler running but not when it wasn't? I suspect that Fiddler is "playing nice" and forcibly closing the connections.


I ran into a similar issue, where WebClient.DownloadFile would timeout when certain web requests happened prior. After searching fruitlessly for a web request response that wasn't properly closed (Using this method), I came across the ServicePointManager.DefaultConnectionLimit property. Setting it higher at the beginning of my application resolved the issue for me, like so:

ServicePointManager.DefaultConnectionLimit = 20

Tags:

C#

.Net

Webclient