简体   繁体   English

上传时webclient超时

[英]webclient timeout while uploading

I know that WebClient doesnot have the property of timeout. 我知道WebClient没有超时属性。 I searched around and found different codes in which you can inherit the webclient from httpwebrequest and set the timeout For Example: 我四处搜索并找到了不同的代码,您可以从httpwebrequest继承webclient并设置超时示例:

   class MyWebClient : WebClient
{
    protected override WebRequest GetWebRequest(Uri address)
    {
        WebRequest request = base.GetWebRequest(address);
        if (request is HttpWebRequest)
        {
            (request as HttpWebRequest).KeepAlive = false;
            (request as HttpWebRequest).Timeout = 25000; //(tried different values)
        }
        return request;
    }
}

But nothing seems to work here. 但这似乎没有任何作用。 The timeout occurs exactly after 100 seconds. 超时发生在100秒之后。 I am trying to upload big file through this client application i made. 我试图通过我做的客户端应用程序上传大文件。 PHP is running on the server side and all timeouts/maxupload values are set. PHP在服务器端运行,并设置所有超时/ maxupload值。

The exception message is : 异常消息是:

the request was aborted the request was canceled

Please help me out. 请帮帮我。

另一个答案中的代码对我有用,我只将第9行更改为:

((HttpWebRequest)request).Timeout = System.Threading.Timeout.Infinite;

The default value of httpWebRequest is 100 seconds so something is not getting set right in the code. httpWebRequest的默认值是100秒,所以在代码中没有设置好的东西。

Have you tried to set .KeepAlive = true; 你试过设置.KeepAlive = true;

MSDN says setting it to false can MSDN表示将其设置为false即可

When using HTTP/1.1, Keep-Alive is on by default. 使用HTTP / 1.1时,默认情况下启用Keep-Alive。 Setting KeepAlive to false may result in sending a Connection: Close header to the server. 将KeepAlive设置为false可能会导致向服务器发送Connection:Close标头。

This would make sense since it seems that you are setting your timeout correctly. 这是有道理的,因为您似乎正确设置了超时。 You can doublecheck here 你可以在这里仔细检查

This SO question also has an answer that links that error message to the keep alive property. 这个 SO问题也有一个答案,将该错误消息链接到保持活动属性。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM