locked
Windows.Web.Http.HttpClient Timeout Option RRS feed

  • Question

  • 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?

    Wednesday, October 23, 2013 7:02 AM

Answers

  • There's a bit of confusion here -- the Windows 8.1 Windows.Web.Http uses the standard Windows Runtime mechanisms for timeout.  Some sample code is:

     var cts = new CancellationTokenSource();
     cts.CancelAfter(timeout);
     var hc = new HttpClient();
     try
     {
      var task = hc.GetStringAsync(new Uri("http://msw")).AsTask(cts.Token);
      var result = await task;
      uiResults.Text = result;
     }
     catch (Exception exception)
     {
      uiResults.Text = "Exception thrown: " + exception.Message;
     }

    CancellationTokenSource is in System.Threading, and is a C# construct.

    The other solution is using the System.Net.Http HttpClient; it's not the preferred HttpClient.  That one does have a GetAsync() where you can pass in the cancellation token.

    If you would like an overall diagram of the Windows.Web.Http family of classes, there's a poster available for download at http://www.microsoft.com/en-us/download/details.aspx?id=40018



    Network Developer Experience Team (Microsoft)

    • Marked as answer by Anne Jing Tuesday, October 29, 2013 9:16 AM
    Thursday, October 24, 2013 7:28 PM

All replies

  • There are used Windows.Web.Http.HttpClient in sample. But also you can use System.Net.Http.HttpClient which has Timeout property.
    Wednesday, October 23, 2013 8:03 AM
  • No, System.Net.Http.HttpClient doesn't have the SSL filter option.

    Wednesday, October 23, 2013 10:15 AM
  • Hi, Pradeep

    You can use the HttpClient.Timeout property to  set or queried the timeout.

    You also 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)
            {
    
            }

    There a link you can refer to see how to use the CancellationTokenSource  to set timeout:

    http://msdn.microsoft.com/en-us/library/windows/apps/jj710176.aspx

    Best Wishes!


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey. Thanks<br/> MSDN Community Support<br/> <br/> Please remember to &quot;Mark as Answer&quot; the responses that resolved your issue. It is a common way to recognize those who have helped you, and makes it easier for other visitors to find the resolution later.

    Thursday, October 24, 2013 2:06 AM
  • There's a bit of confusion here -- the Windows 8.1 Windows.Web.Http uses the standard Windows Runtime mechanisms for timeout.  Some sample code is:

     var cts = new CancellationTokenSource();
     cts.CancelAfter(timeout);
     var hc = new HttpClient();
     try
     {
      var task = hc.GetStringAsync(new Uri("http://msw")).AsTask(cts.Token);
      var result = await task;
      uiResults.Text = result;
     }
     catch (Exception exception)
     {
      uiResults.Text = "Exception thrown: " + exception.Message;
     }

    CancellationTokenSource is in System.Threading, and is a C# construct.

    The other solution is using the System.Net.Http HttpClient; it's not the preferred HttpClient.  That one does have a GetAsync() where you can pass in the cancellation token.

    If you would like an overall diagram of the Windows.Web.Http family of classes, there's a poster available for download at http://www.microsoft.com/en-us/download/details.aspx?id=40018



    Network Developer Experience Team (Microsoft)

    • Marked as answer by Anne Jing Tuesday, October 29, 2013 9:16 AM
    Thursday, October 24, 2013 7:28 PM