jbeldock jbeldock - 4 years ago 119
C# Question

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

I've got a simple C# system tray app which uses

to download several files, each in the range of 3-15MB. The WebClient is downloading via HTTPS, and I control the servers from which the downloads are taking place. The files download just fine from a web browser via direct path, and the WebClient is perfectly able to process
methods (i.e., download short text files to string) from this server.

Weirdly, three things have happened in 24 hours, and I'm suspecting some kind of machine-specific config or caching, but I wonder what others might know about
's innards which might permit them to hazard a guess:

  1. Development machine #1 had no trouble running this code in Debug mode and downloading the files (call them MegabyteFile1 and MegabyteFile2) until Friday. Downloads took 3-5 seconds, maximum.

  2. Suddenly, Development machine #1 stopped downloading MegabyteFile1 on Friday afternoon. Behavior is as follows:

    • WebClient.DownloadFile()
      hangs indefinitely

    • The file at
      is created but has 0 bytes.

    • Main thread blocks indefinitely.

    • Must exit application.

  3. Other development machines with identical OS configurations run the code just fine and download successfully.

  4. Manually downloading these files using Chrome or Internet Explorer on DevelopmentMachine1 works just fine, with and without the query parameters.

  5. DevelopmentMachine1 will download the file if Fiddler is running (even without having the Fiddler HTTPS certificate impostors installed). In other words, if I try to sniff the wire on this machine, everything works fine.

The pertinent section of the download code reads as follows:

using (WebClient client2 = new WebClient())
client2.DownloadFile(String.Format("{0}?{1}", thePath, queryParams), targetFileFullPath);

I've tried the usual sysadmin stuff on this box: rebooted it, cleared the Internet Explorer cache and temp directories, etc.

I know I need to change my strategy for dealing with hung downloads, as I'm sure there will be downloads that hang in the real world. But I'm not confident I'll think of all the corner cases if I don't understand what is causing this particular failure mode. Anyone know enough about what's going on in
to hazard a guess as to why this machine is "special"?


Answer Source

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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download