Skip to content

Instantly share code, notes, and snippets.

@maestrow
Last active March 10, 2017 09:40
Show Gist options
  • Save maestrow/7bf9f4458d4b77e77c80fbfd4b4b68da to your computer and use it in GitHub Desktop.
Save maestrow/7bf9f4458d4b77e77c80fbfd4b4b68da to your computer and use it in GitHub Desktop.
HTTP Client Authentication
using (HttpClient client = new HttpClient())
{
client.DefaultRequestHeaders.Accept.Add(
new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json"));
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic",
Convert.ToBase64String(
System.Text.ASCIIEncoding.ASCII.GetBytes(
string.Format("{0}:{1}", "login", "password"))));
using (HttpResponseMessage response = client.GetAsync(
"https://url").Result)
{
response.Dump();
}
}
string credentials = Convert.ToBase64String(Encoding.UTF8.GetBytes("login:password"))
client.Headers[HttpRequestHeader.Authorization] = "Basic " + credentials
var credentials = new NetworkCredential("login", "password", "domain");
var handler = new HttpClientHandler { Credentials = credentials };
using (var http = new HttpClient(handler))
{
var response = http.GetAsync("https://url").Result;
response.Dump();
}
HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create("https://url");
request.Method = "GET";
request.UseDefaultCredentials = false;
request.PreAuthenticate = false;
request.AllowAutoRedirect = false;
request.Credentials = new NetworkCredential("login", "password", "domain");
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
response.Dump();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment