title: Parsing UTF8 JSON response from server
body: <p>I am facing a weird problem parsing a JSON response from my server. It has been working fine for the last months when getting the response <strong>(with Content-Type: text/html)</strong> this way:</p>
  
  <pre><code>string response = "";
  using (var client = new System.Net.Http.HttpClient())
  {
      var postData = new System.Net.Http.FormUrlEncodedContent(data);
      var clientResult = await client.PostAsync(url, postData);
      if(clientResult.IsSuccessStatusCode)
      {
          response = await clientResult.Content.ReadAsStringAsync();
      }
  }
  //Parse the response to a JObject...
  </code></pre>
  
  <p>But when receiving a response with <strong>Content-Type: text/html; charset=utf8</strong> it throws an exception that <strong>Content-Type is invalid</strong>.</p>
  
  <p><code>Exception message: The character set provided in ContentType is invalid. Cannot read content as string using an invalid character set.</code></p>
  
  <p>So I changed this:</p>
  
  <pre><code>response = await clientResult.Content.ReadAsStringAsync();
  </code></pre>
  
  <p>to this:</p>
  
  <pre><code>var raw_response = await clientResult.Content.ReadAsByteArrayAsync();
  response = Encoding.UTF8.GetString(raw_response, 0, raw_response.Length);
  </code></pre>
  
  <p>Now I can get the response without no exceptions but when parsing it, it throws a parsing exception. While debugging I got this: (I changed the response to a shorter one for testing purposes)</p>
  
  <pre><code>var r1 = await clientResult.Content.ReadAsStringAsync();
  var r2 = Encoding.UTF8.GetString(await clientResult.Content.ReadAsByteArrayAsync(), 0, raw_response.Length);
  System.Diagnostics.Debug.WriteLine("Length: {0} - {1}", r1.Length, r1);
  System.Diagnostics.Debug.WriteLine("Length: {0} - {1}", r2.Length, r2);
  
  //Output
  Length: 38 - {"version":1,"specialword":"C\u00e3o"}
  Length: 39 - {"version":1,"specialword":"C\u00e3o"}
  </code></pre>
  
  <p>The JSON response format seems correct in both cases but the length is different and I could't figured out why. When copying this to notepad++ to spot hidden characters a <strong>?</strong> appeared from nowhere.</p>
  
  <pre><code>Length: 38 - {"version":1,"specialword":"C\u00e3o"}
  Length: 39 - ?{"version":1,"specialword":"C\u00e3o"}
  </code></pre>
  
  <p>This <strong>?</strong> is obviously throwing the parsing exception but I don't know why <code>Encoding.UTF8.GetString</code> is causing that.</p>
  
  <p>I have been battling with this for the last hours and I really need some help.</p>