AndyP
AndyP

Reputation: 607

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

Upvotes: 12

Views: 26329

Answers (2)

Sebastian Inones
Sebastian Inones

Reputation: 1701

I hope this may help in your specific scenario or others with a similar situation. In my case, my header was containing non ASCII characters as one of the Headers values was generated by creating a sha256 HMAC. That was a requirement from the service I was trying to send my request. So, what I had to do in order to encode those characters into ASCII was just this:

//hashedSignature is the one containing NON ASCII charcters!
string MessageSignatureValue = System.Text.Encoding.ASCII.GetString(hashedSignature); 
httpClient.DefaultRequestHeaders.Add(MessageSignaturField, MessageSignatureValue);

I have just included the importants pieces of code.

So, System.Text.Encoding.ASCII.GetString() method did the trick!

Again, I hope it helps.

Upvotes: 3

Yigit Yuksel
Yigit Yuksel

Reputation: 1170

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".

Upvotes: 4

Related Questions