Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 300
Pradeep devendran Avatar asked Oct 23 '13 07:10

Pradeep devendran


Video Answer


1 Answers

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

}
like image 123
Cyprien Autexier Avatar answered Oct 02 '22 08:10

Cyprien Autexier