Web客户端UploadFile错误 [英] WebClient UploadFile errors

查看:191
本文介绍了Web客户端UploadFile错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将文件上传到使用System.Net.WebClient.UploadFile一个Web服务器,但我不断收到一个WebException。具体来说,我得到3个错误。我不知道为什么我没有收到同样的错误,但他们似乎都与基于我在网上找到可以。

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.

  • 请求已中止:请求已被取消
  • 连接关闭。请重试。
  • 在一个现有的连接被强行关闭远程主机。

有似乎有点随机(不总是相同的文件,的时间量,或任何其他图案,我可以弄清楚)。此外,这不会发生我的工作网(的上传时间不到2秒),但是从家里发生过DSL(在上传约需2分钟)。

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).

从我在网上找到的,这些错误必须是与保持有效指示。不幸的是Web客户端不提供任何方式把这些关闭(我不知道我是否想反正,因为这是一个性能特性)。

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).

我想这可能是与超时,但我想不出为什么。服务器是ASP.Net MVC和超时被设置为一个小时。

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).

一些背景,这是一个WinForms应用程序,上传照片到服务器。该服务器是一个ASP.Net MVC应用程序。该客户端已正确地工作了很长一段时间,但现在没有,因为我切换到ASP.Net MVC(这是用传统的ASP与SA FileUp)。该客户端不仅改变以适应新的网址,比它是pretty的大同小异其他。

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.

推荐答案

除了请求已中止:请求已被取消。如果Web客户端时间被抛出了一个文件传输过程中。如果没有文件传输正在发生,你会得到操作超时代替。

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.

超时发生在客户端上。 Web客户端不允许设置超时和默认的HttpWebRequest(Web客户端使用什么样的)为100秒。我想我必须弄清楚如何在使用的HttpWebRequest获得进展。我还必须弄清楚为什么ASP.Net文件传输比使用SAFileUp与传统的ASP更慢。

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.

更新:我创建了一个WebHelper类,它的WebClient的地方,但提供了更多的访问底层的WebRequest的必要特征。它还提供了一系列的附加功能在Web客户端。如果你有兴趣,这是在在codePLEX四海商舟的项目可用。

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.

这篇关于Web客户端UploadFile错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆