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
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)
{
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With