Pradeep devendran
Pradeep devendran

Reputation: 489

Windows.Web.Http.HttpClient Timeout Option

Due to SSL certificate issue we are using "Windows.Web.Http.HttpClient" API in my app service layer.

I referred below sample for my project.

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

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

Upvotes: 6

Views: 4772

Answers (1)

Cyprien Autexier
Cyprien Autexier

Reputation: 1998

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 : With 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)
{

}

Upvotes: 14

Related Questions