Skip to content

Instantly share code, notes, and snippets.

@mattdot
Created July 16, 2011 06:39
Show Gist options
  • Save mattdot/1086078 to your computer and use it in GitHub Desktop.
Save mattdot/1086078 to your computer and use it in GitHub Desktop.
WCF Web API: Extension methods for requesting a response in JSON, and for setting Basic auth headers
public static class HttpClientExtensionMethods
{
public static void SetBasicAuth(this HttpClient httpClient, string userName, string password)
{
var byteArray = Encoding.ASCII.GetBytes(userName + ":" + password);
httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", Convert.ToBase64String(byteArray));
}
public static void AcceptJson(this HttpClient httpClient)
{
var jsonMediaType = new MediaTypeWithQualityHeaderValue("application/json");
httpClient.DefaultRequestHeaders.Accept
.Add(jsonMediaType);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment