i am using HttpClient to send request, i want to use my custom request headers using HttpClient in GET Method?
Here is my code:
public HttpResponseMessage Get(string url, List<KeyValuePair<string, string>> headers = null)
{
HttpRequestMessage request = new HttpRequestMessage()
{
RequestUri = new Uri(url),
Method = HttpMethod.Get,
};
if (headers != null && headers.Count > 0)
{
foreach (var header in headers)
{
request.Headers.Add(header.Key, header.Value);
}
}
HttpResponseMessage response = httpClient.SendAsync(request).Result;
return response;
}
But it threw an error at request.Headers.Add(header.Key, header.Value);
Below is the error message:
Misused header name. Make sure request headers are used with HttpRequestMessage, response headers with HttpResponseMessage, and content headers with HttpContent objects.
Any help would be appreciated
header.Keyandheader.Valuewhen you get the exception?Content-Typeis a bug. If you want to request a specific content type use theAcceptheader