That isn't how you use HttpClient
and therefore you could be running into issues with the client itself. HttpClient
has a couple of properties on it including BaseAddress
and DefaultRequestHeaders
. However these are designed for all calls to that client and not per-request. If you attempt to use these properties after the client has been used the first time you are going to get into trouble. So, for example, the following call I would not expect to work.
var client = new HttpClient() {
BaseAddress = new Uri("...")
};
client.GetAsync(...);
client.BaseAddress = new Uri("...");
client.DefaultRequestHeaders.Add(...);
To properly use HttpClient
you need to create an instance and set the base address along with any request headers that should be sent with all requests (and therefore are unchanged). This is normally very high level stuff like using JSON for everything.
public class MyService
{
public MyService ( string url )
{
_client = new HttpClient() {
BaseAddress = new Uri(url) //Ensure the URL ends with a slash...
};
_client.DefaultRequestHeaders.Add(...);
}
public Task DoSomething ()
{
return _client.GetAsync(...);
}
//Ignoring HttpClient lifetime for this discussion
private readonly HttpClient _client;
}
If you need to manipulate the headers on a per-request basis then the correct approach is to create an HttpRequestMessage and use it instead.
public Task DoSomething ( int someValue )
{
//Second parameter is the URL after the base address...
using (var msg = new HttpRequestMessage(HttpMethod.Get, ""))
{
//Add per-request headers
msg.Headers.Add(...);
return _client.SendAsync(msg);
};
}
Beyond that we can only assume you're passing the right HTTP headers. Showing us the Postman info would be good.