Skip to content

Instantly share code, notes, and snippets.

@avanderhoorn
Created November 25, 2014 22:06
Show Gist options
  • Save avanderhoorn/08b792b1626bf4e5aba8 to your computer and use it in GitHub Desktop.
Save avanderhoorn/08b792b1626bf4e5aba8 to your computer and use it in GitHub Desktop.
HttpClient test
_httpClient.GetAsync("http://localhost:15316/Glimpse/Agent")
.ContinueWith(requestTask =>
{
// Get HTTP response from completed task.
HttpResponseMessage response = requestTask.Result;
// Check that response was successful or throw exception
response.EnsureSuccessStatusCode();
// Read response asynchronously as JsonValue and write out top facts for each country
response.Content.ReadAsAsync<string>().ContinueWith(readTask =>
{
var result = readTask.Result;
});
});
@avanderhoorn
Copy link
Author

The above errors with the following on line 5:

Unable to connect to the remote server

What is the typical way of handling this type of exception?

@darrelmiller
Copy link

For connection failures, disconnects, timeouts and cancellations you have to do a try/catch around the GetAsync method.

@avanderhoorn
Copy link
Author

Great, thanks mate!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment