This is based on a stripped down version of something I implemented at work and modified (but not tested) to work with JSON (based on some other answers here and on the web):
HttpClient client = new HttpClient();
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
HttpResponseMessage response = client.GetAsync("http://somedomain.com/serviceAddress").Result;
string responseContent = response.Content.ReadAsStringAsync().Result;
There are numerous ways to do this, but the above code demonstrates how easy it is (or should be, at least) to do this.
Note that I used the Result
property for the async calls; if you're making this call from within a method marked as async
, you could also do:
HttpResponseMessage response = await client.GetAsync("http://somedomain.com/serviceAddress");
string responseContent = await response.Content.ReadAsStringAsync();
HttpClient
is in the System.Net.Http
namespace.