4

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.

M.Babcock
  • 18,753
  • 6
  • 54
  • 84
faraz ali
  • 115
  • 2
  • 7
  • if keepalive is true do you get the same issue. False can send a connection close header to the server – sealz Feb 10 '12 at 18:36

2 Answers2

3

The code in the other answer worked for me, I only changed line 9 to:

((HttpWebRequest)request).Timeout = System.Threading.Timeout.Infinite;
Gedeon
  • 742
  • 9
  • 13
  • 3
    `( as ).` is a very bad practice; if `` is not of type ``, it will throw a `NullReferenceException`, which isn't helpful at all when diagnosing a problem. Use a direct cast instead: `((HttpWebRequest)request).Timeout` – Thomas Levesque Jan 07 '14 at 17:21
0

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.

Community
  • 1
  • 1
sealz
  • 5,348
  • 5
  • 40
  • 70