40

I have the following implementation. And the default timeout is 100 seconds.

I wonder how can I able to change the default timeout?

HttpService.cs

public class HttpService : IHttpService
{

   private static async Task GoRequestAsync<T>(string url, Dictionary<string, object> parameters, HttpMethod method,
        Action<T> successAction, Action<Exception> errorAction = null, string body = "")
        where T : class
    {
        using (var httpClient = new HttpClient(new HttpClientHandler()))
        {

        }
    }
 }
casillas
  • 16,351
  • 19
  • 115
  • 215

3 Answers3

72

The default timeout of an HttpClient is 100 seconds.


HttpClient Timeout

You can adjust to your HttpClient and set a custom timeout duration inside of your HttpService.

httpClient.Timeout = 5000;


HttpClient Request Timeout

You could alternatively define a timeout via a cancellation token CancellationTokenSource

using (var cts = new CancellationTokenSource(new TimeSpan(0, 0, 5))
{
    await httpClient.GetAsync(url, cts.Token).ConfigureAwait(false);
}

A few notes:

  1. Making changes inside of the HttpClient will affect all requests. If you want to make it per request you will need to pass through your desired timeout duration as a parameter.
  2. Passing an instance of CancellationTokenSource will work if it's timeout is lower than Timeout set by the HttpClient and HttpClient's timeout is not infinite. Otherwise, the HttpClient's timeout will take place.
Robert Taylor
  • 2,860
  • 3
  • 21
  • 19
Plac3Hold3r
  • 5,062
  • 1
  • 16
  • 21
  • 1
    What would be the side effect if I increase from 100 seconds to 10 minutes? any drawback? – casillas Jan 08 '18 at 19:00
  • 5
    Only a longer timeout period. You could also set the timeout to `httpClient.Timeout = Timeout.InfiniteTimeSpan;` if you do not want the request to timeout at all. – Plac3Hold3r Jan 08 '18 at 20:11
  • I can set as `client.Timeout = new TimeSpan(0,0,500);` , then what is the use of `CancellationTokenSource` ? – Shaiju T Feb 08 '19 at 12:07
  • 6
    @stom The `CancellationTokenSource` would allow you to cancel the request prior to the timeout being triggered. The example would be if a user navigates away and you no longer need the requested data. You can then cancel the request. – Plac3Hold3r Feb 08 '19 at 20:44
  • @Plac3Hold3r , Good example :) Appreciate. – Shaiju T Feb 11 '19 at 09:30
  • Can you please elaborate this ` If you want to make it per request you will need to pass through your desired timeout duration as a parameter.` how do we do this? – Naveed Ahmed Feb 18 '21 at 18:33
  • What if I set timeout on HttpClient from HttpClient pool? Is timeout setup reset once the call is completed and HttpClient is returned into the pool? – Kool Dec 30 '21 at 11:32
31

client.Timeout = 5*1000; doesnt work because client.Timeout expects something of type: System.TimeSpan

I changed the Timeout value using:

client.Timeout = TimeSpan.FromSeconds(10); // Timeout value is 10 seconds

You can use other methods as well:

Just for FYI:

Default value of Timeout property is 100 seconds

SU7
  • 1,586
  • 1
  • 18
  • 26
  • Hi what is the max Timeout Value for this case? – toha May 13 '22 at 03:25
  • @toha it seems the max value can be set to InfiniteTimeSpan. Please check this doc: https://learn.microsoft.com/en-us/dotnet/api/system.net.http.httpclient.timeout?view=net-6.0 – SU7 May 13 '22 at 05:54
2

Since we don't see any task created with a timeout i cannot help.

But if you are using a System.Net.Http under the hood of your application than MSDN says:

The default value is 100,000 milliseconds (100 seconds).

You can than change the value of the HttpClient.Timeout property

clent.Timeout = 5*1000;
Dolev
  • 654
  • 11
  • 20