title: Request headers must contain only ASCII characters while using c# http client?
body: <p>I am using <code>HttpClient</code> to make a POST call by passing header but at some point of time I am getting an error as:</p>
  <pre><code>Request headers must contain only ASCII characters.
  </code></pre>
  <p>With stacktrace as:</p>
  <pre><code>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)
  </code></pre>
  <p>Here is my code:</p>
  <pre><code>public HttpWrapper(string endpoint, Func&lt;IDictionary&lt;string, string&gt;&gt; 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);
          }
      }
  }
  </code></pre>
  <p>Do I need to do something with <code>header.Value</code> 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?</p>
  <p><strong>Update</strong></p>
  <p>I got header value like this today and it threw same exception since <code>HttpUtility.HtmlEncode</code> didn't do anything on it. Also I am not sure what is this character <code>&lt;0x94&gt;</code>? Any thoughts why it is happening?</p>
  <p>Also I am not sure</p>
  <p><a href="https://i.stack.imgur.com/XXtgJ.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/XXtgJ.png" alt="enter image description here" /></a></p>