I am building a class library to interact with an API. I need to call the API and process the XML response. I can see the benefits of using HttpClient
for Asynchronous connectivity, but what I am doing is purely synchronous, so I cannot see any significant benefit over using HttpWebRequest
.
If anyone can shed any light I would greatly appreciate it. I am not one for using new technology for the sake of it.
but what i am doing is purely synchronous
You could use HttpClient
for synchronous requests just fine:
using (var client = new HttpClient())
{
var response = client.GetAsync("http://google.com").Result;
if (response.IsSuccessStatusCode)
{
var responseContent = response.Content;
// by calling .Result you are synchronously reading the result
string responseString = responseContent.ReadAsStringAsync().Result;
Console.WriteLine(responseString);
}
}
As far as why you should use HttpClient
over WebRequest
is concerned, well, HttpClient
is the new kid on the block and could contain improvements over the old client.