简体   繁体   中英

webclient timeout while uploading

I know that WebClient doesnot have the property of timeout. I searched around and found different codes in which you can inherit the webclient from httpwebrequest and set the timeout For Example:

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

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.

Have you tried to set .KeepAlive = true;

MSDN says setting it to false can

When using HTTP/1.1, Keep-Alive is on by default. Setting KeepAlive to false may result in sending a Connection: Close header to the server.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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