Search code examples
c#dotnet-httpclient

Request headers must contain only ASCII characters while using c# http client?


I am using HttpClient to make a POST call by passing header but at some point of time I am getting an error as:

Request headers must contain only ASCII characters.

With stacktrace as:

at System.Net.Http.HttpConnection.WriteStringAsync(String s)
   at System.Net.Http.HttpConnection.WriteHeadersAsync(HttpHeaders headers, String cookiesFromContainer)
   at System.Net.Http.HttpConnection.SendAsyncCore(HttpRequestMessage request, CancellationToken cancellationToken)
   at System.Net.Http.HttpConnectionPool.SendWithNtConnectionAuthAsync(HttpConnection connection, HttpRequestMessage request, Boolean doRequestAuth, CancellationToken cancellationToken)
   at System.Net.Http.HttpConnectionPool.SendWithRetryAsync(HttpRequestMessage request, Boolean doRequestAuth, CancellationToken cancellationToken)
   at System.Net.Http.RedirectHandler.SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
   at System.Net.Http.DiagnosticsHandler.SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
   at System.Net.Http.HttpClient.FinishSendAsyncBuffered(Task`1 sendTask, HttpRequestMessage request, CancellationTokenSource cts, Boolean disposeCts)

Here is my code:

public HttpWrapper(string endpoint, Func<IDictionary<string, string>> NewHeader)
{
    _httpClient = new HttpClient();
    _httpClient.BaseAddress = new Uri(endpoint);

    if (NewHeader != null)
    {
        var headers = NewHeader();
        foreach (var header in headers)
        {
            _httpClient.DefaultRequestHeaders.TryAddWithoutValidation(header.Key, header.Value);
        }
    }
}

Do I need to do something with header.Value to fix this issue? I was reading online so looks like I need to use Utf-8 here but not sure on how to do it properly?

Update

I got header value like this today and it threw same exception since HttpUtility.HtmlEncode didn't do anything on it. Also I am not sure what is this character <0x94>? Any thoughts why it is happening?

Also I am not sure

enter image description here


Solution

  • You can try to encode header values to HTML:

    _httpClient.DefaultRequestHeaders.TryAddWithoutValidation(header.Key, HttpUtility.HtmlEncode(header.Value));
    

    Your string, Ergänzendes will be "Erg&#228;nzendes".