Created
July 16, 2011 06:39
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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