We have an external app that has an Web API/Service to use from our internal software or different apps.
Anywho I just can't seem to make the HttpClient work. It works with the older WebClient, or with RestClient among other tools.I just can'y make it work with HttpClient and it really bugs me, as in I don;t know why. The IP and API key are fake.
So with RestClient and other techonlogies it works:
public async Task<List<Employee>> LoadEmployees()
{
var client = new estClient("http://99.99.99.99:8080/api/v2/employee/");
var request = new RestRequest(Method.GET);
request.AddHeader("cache-control", "no-cache");
request.AddHeader("accept-encoding", "gzip, deflate");
request.AddHeader("Host", "99.99.99.99:8080");
request.AddHeader("Cache-Control", "no-cache");
request.AddHeader("Accept", "*/*");
request.AddHeader("Authorization", "apwerfhafdh>0923817adfhhasfd<9");
request.AddHeader("Content-Type", "application/json");
IRestResponse responseData = await client.ExecuteGetTaskAsync(request);
if (responseData.StatusCode == System.Net.HttpStatusCode.OK)
{
List<Employee> emps = JsonConvert.DeserializeObject<List<Employee>>(responseData.Content);
return emps;
}
else
{
throw new Exception($"{responseData.StatusCode} : {responseData.StatusDescription}");
}
}
but with HttpClient I can't manage to make the Authorization part work.
What I have tried:
Client= new HttpClient();
Client.DefaultRequestHeaders.Accept.Clear();
Client.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json"));
Client.DefaultRequestHeaders.Add("Accept", "*/*");
Client.DefaultRequestHeaders.Add("accept-encoding", "gzip, deflate");
Client.DefaultRequestHeaders.Add("Authorization", "X-ApiKey apwerfhafdh>0923817adfhhasfd<9");