Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
946 views
in Technique[技术] by (71.8m points)

.net - WebClient UploadFile errors

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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, it is available in the BizArk project on CodePlex.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...