Windows.Web.Http.HttpClient wait option

Due to a problem with the SSL certificate we use the API "Windows.Web.Http.HttpClient" in my service.

I referred below to a sample of my project.

http://code.msdn.microsoft.com/windowsapps/HttpClient-sample-55700664

How can we implement the timeout parameter in the API "Windows.Web.Http.HttpClient"

+4
source share
1 answer

You can use a CancellationTokenSource with a timeout.

        HttpClient client = new HttpClient();
        var cancellationTokenSource = new CancellationTokenSource(2000); //timeout
        try
        {
            var response = await client.GetAsync("https://test.example.com", cancellationTokenSource.Token);
        }
        catch (TaskCanceledException ex)
        {

        }

Edit: Using Windows.Web.Http.HttpClient you should use the AsTask () extension method:

HttpClient client = new HttpClient();
System.Threading.CancellationTokenSource source = new System.Threading.CancellationTokenSource(2000);
try
{
    client.GetAsync(new Uri("http://example.com")).AsTask(source.Token);
}
catch(TaskCanceledException ex)
{

}
+14
source

All Articles