views:

425

answers:

1

I am trying to upload files to a web server using System.Net.WebClient.UploadFile but I keep getting a WebException. Specifically, I am getting 3 errors. I have no idea why I am not getting the same error, but they all seem to be related based on what I found online.

  • The request was aborted: The request was canceled.
  • Connection closed. Try again.
  • An existing connection was forcibly closed by the remote host.

It seems somewhat random (not always the same file, amount of time, or any other pattern that I can figure out). Also This doesn't happen on my work network (the uploads take less than 2 seconds), but does happen from home over a DSL (the uploads take about 2 minutes).

From what I have found online, these errors have something to do with keep-alives. Unfortunately WebClient doesn't provide any way to turn these off (I'm not sure if I would want to anyway since this is a performance feature).

I think it might have something to do with timeouts, but I can't figure out why. The server is ASP.Net MVC and the timeout is set to an hour.

    <httpRuntime
        maxRequestLength="10024" 
        executionTimeout="3600"
        /><!-- 10024 = 10MB, 3600 = 1hr -->

I'm interested in both ways to fix this problem so it doesn't happen and also recovery techniques (simply making the request again doesn't seem to be effective).

Some background, this is for a WinForms application that uploads photos to the server. The server is an ASP.Net MVC application. The client has worked correctly for a long time but is now failing since I switched it to ASP.Net MVC (it was using classic ASP with SA FileUp). The client side only changed to accomodate the new URLs, other than that it is pretty much the same.

A: 

The exception "The request was aborted: The request was canceled." is thrown if the WebClient times out during a file transfer. If no file transfer is taking place, you will get "The operation has timed out" instead.

The timeout is occurring on the client. WebClient does not allow you to set the timeout and the default for HttpWebRequest (what WebClient uses) is 100 seconds. I guess I will have to figure out how to get the progress when using HttpWebRequest. I will also have to figure out why ASP.Net file transfer is slower than using SAFileUp with classic ASP.

UPDATE: I've created a WebHelper class that takes the place of WebClient but provides more access to the necessary features of the underlying WebRequest. It also provides a bunch of additional capabilities over the WebClient. If you are interested, you can read more on my blog, http://www.redwerb.com/post/BizArk-Feature-Spotlight-WebHelper.aspx.

Brian