Skip to content

Instantly share code, notes, and snippets.

@dantheman213
Created July 30, 2015 23:41
Show Gist options
  • Star 6 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save dantheman213/a39684b7824e30f445e5 to your computer and use it in GitHub Desktop.
Save dantheman213/a39684b7824e30f445e5 to your computer and use it in GitHub Desktop.
C# HTTP GET request synchronous example
using System.Net.Http;
using (var client = new HttpClient())
{
var url = "http://google.com/api-example";
var response = client.GetAsync(url).Result;
if (response.IsSuccessStatusCode)
{
// by calling .Result you are performing a synchronous call
var responseContent = response.Content;
// by calling .Result you are synchronously reading the result
string responseString = responseContent.ReadAsStringAsync().Result;
Console.WriteLine(responseString);
}
}
@UweKeim
Copy link

UweKeim commented Apr 23, 2018

Isn't calling .Result a potential source for deadlocks and AsyncBridge should be used instead?

@rhysjtevans
Copy link

Isn't calling .Result a potential source for deadlocks and AsyncBridge should be used instead?

Yes, coincidently watched this today - found it very interesting and insightful.
https://www.youtube.com/watch?v=av5YNd4X3dY

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