Using C# HttpClient() from Sync and Async code

The .NEt 4.5 C# System.Net.Http.HttpClient() is a very nice http client implementation, but can be tricky to use if you (like me) are not a trained asynchronous programming coder. So here is a quick cheat sheet on how to work around the Task<>, async and await methods when using the HttpClient().

EXAMPLE 1: HTTPCLIENT GET WITH RETURN VALUE:

THE GET METHOD:

public static async Task<string> Get(string queryString)
{
  using(var httpClient = new HttpClient())
  {
    string authUserName = "user"
    string authPassword = "password"
    string url = "https://someurl.com";

    // If you do not have basic authentication, you may skip these lines
    var authToken = Encoding.ASCII.GetBytes($"{authUserName}:{authPassword}");
    httpClient.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Basic", Convert.ToBase64String(authToken));

    // The actual Get method
    using (var result = await httpClient.GetAsync($"{url}{queryString}"))
    {
      string content = await result.Content.ReadAsStringAsync();
      return content;
    }
  }
}

THE USAGE:

// From synchronous code
string queryString = "?hello=world";
string result = Get(queryString).Result;

// From asynchronous code
string queryString = "?hello=world";
string result = await Get(queryString);

 

EXAMPLE 2: HTTPCLIENT PUT “FIRE-AND-FORGET” WITHOUT RETURN VALUE:

THE PUT METHOD:

public static async Task Put(string postData)
{
  using(var httpClient = new HttpClient())
  {
    string authUserName = "user";
    string authPassword = "password"
    string url = "https://someurl.com";
        
    // If you have no basic authentication, you can skip thses lines
    var authToken = Encoding.ASCII.GetBytes($"{authUserName}:{authPassword}");
    httpClient.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Basic", Convert.ToBase64String(authToken));
    
    // The actual put method including error handling
    using(var content = new StringContent(postData))
    {
      var result = await httpClient.PutAsync($"{url}", content);
      if (result.StatusCode == HttpStatusCode.OK)
      {
        return;
      }
      else
      {
        // Do something with the contents, like write the statuscode and
        // contents to a log file
        string resultContent = await result.Content.ReadAsStringAsync();
        // ... write to log
      }
    }
  }
}

THE USAGE:

// You can call the method from asynchronous
// and it will actually run asynchronous. In this fire-and-forget 
// pattern, there is no need to wait for the answer
Put("data");

// ... but if you will wait, simply call ".Wait()":
Put("data").Wait();

MORE TO READ:

 

Advertisements

About briancaos

Developer at Pentia A/S since 2003. Have developed Web Applications using Sitecore Since Sitecore 4.1.
This entry was posted in .net, c#, General .NET and tagged , , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s