Been elaborating a bit with HttpClient for building a rest client. But I can\'t figure out, nor find any examples on how to authenticate towards the server. Most likely I wi
I believe this is a bit old, but for anyone looking for an updated answer, I used this code when I built my test server:
using (var client = new HttpClient()) { client.BaseAddress = new Uri("http://myServer/api/Person"); client.DefaultRequestHeaders.Accept.Clear(); client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); var byteArray = Encoding.ASCII.GetBytes($"{UserName}:{ApiPassword}"); client.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Basic", Convert.ToBase64String(byteArray));
Or this works too:
using (var http = new HttpClient()) { // byteArray is username:password for the server var byteArray = Encoding.ASCII.GetBytes("myUserName:myPassword"); http.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Basic", Convert.ToBase64String(byteArray)); string uri = "http://myServer/api/people" ; var response = await http.GetStringAsync(uri); List<Person> agencies = JsonConvert.DeserializeObject<List<Person>>(response); foreach (Person person in people) { listBox1.Items.Add(person.Name); } }
For what it is worth, nothing using HttpClientHandler worked, at least not for trying to make an authenticated call to the CouchDB API that requires server admin credentials.
This worked for me:
using( var client = new HttpClient() )
{
var byteArray = Encoding.ASCII.GetBytes("MyUSER:MyPASS");
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", Convert.ToBase64String(byteArray));
....
}
As outlined in the answer here:
How to use credentials in HttpClient in c#?
The HttpClient library did not make it into .Net 4. However it is available here http://nuget.org/List/Packages/HttpClient. However, authentication is done differently in this version of HttpClient.
var httpClient = new HttpClient();
httpClient.DefaultRequestHeaders.Authorization
= new AuthenticationHeaderValue("basic","...");
or
var webRequestHandler = new WebRequestHandler();
CredentialCache creds = new CredentialCache();
creds.Add(new Uri(serverAddress), "basic",
new NetworkCredential("user", "password"));
webRequestHandler.Credentials = creds;
var httpClient = new HttpClient(webRequestHandler);
And be warned, this library is going to get updated next week and there are minor breaking changes!
It only works when DefaultRequestHeaders
is specified. It doesnt work any other way.
I just downloaded 0.3.0 it has indeed be removed. It's now on HttpClientChannel
HttpClient client = new HttpClient(...);
var channel = new HttpClientChannel();
channel.Credentials = new NetworkCredential(...);
client.Channel = channel;
If not explicitly specified it uses a default instance of HttpClientChannel
.
UPDATE: this is now invalid for .Net 4.5; see correct answer below: https://stackoverflow.com/a/15034995/58391
All these are out of date. The final way to do it is as follows:
var credentials = new NetworkCredential(userName, password);
var handler = new HttpClientHandler { Credentials = credentials };
using (var http = new HttpClient(handler))
{
// ...
}