Skip to content

Instantly share code, notes, and snippets.

@rswilley
Forked from dantheman213/HttpGetRequestSync.cs
Created October 24, 2017 14:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rswilley/8d02e044ab169527814593e691de1d83 to your computer and use it in GitHub Desktop.
Save rswilley/8d02e044ab169527814593e691de1d83 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);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment