简体   繁体   中英

Is the Content-Type charset not exposed from HttpResponseMessage?

I'm converting some code from using HttpWebRequest to HttpClient . One problem I'm having is getting the charset from the content-type response header.

When using HttpWebRequest , the charset is exposed in the HttpWebResponse.CharacterSet property, like this

using (WebResponse response = await this.webRequest.GetResponseAsync())
{
     string characterSet = ((HttpWebResponse)response).CharacterSet;

You can also get to it from WebResponse.ContentType property or from the content-type header in HttpWebResponse.Headers .

Using HttpClient , the charset seems to be missing from the ContentType header.

Here's the code that I'm using for HttpClient :

using (HttpClient httpClient = new HttpClient(httpClientHandler))
{
    using (HttpResponseMessage httpResponseMessage = await httpClient.GetAsync(uri, HttpCompletionOption.ResponseContentRead))
    {
        charset = httpResponseMessage.Content.Headers.ContentType.CharSet;

The CharSet property is always null . HttpResponseMessage has a Headers property but it doesn't contain the content-type header. HttpResponseMessage.Content also has a Headers property, which does appear to contain the content-type header, but that header shows "Content-Type: text/html" - it doesn't have the charset portion.

Using the first approach with HttpWebResponse for the same url, I get the charset portion of the Content-Type header. Am I missing something?

I was looking to emit the charset inside a HttpResponseMessage and since your question is the first on google and that i found the answer several pages below, here is the code

httpResponseMessage.Content.Headers.ContentType = new MediaTypeHeaderValue("text/csv");
httpResponseMessage.Content.Headers.ContentType.CharSet = Encoding.UTF8.HeaderName;
httpResponseMessage.Content.Headers.Add("CodePage", Encoding.UTF8.CodePage.ToString());

您可以通过以下方式获取:

var contentType = response.Content.Headers.GetValues("Content-Type").First());

I believe the Content-Type header returned from the server would have to contain the 'charset' like 'text/html;charset=UTF-8' in order for it to show up in the CharSet property. Checking the raw response in a tool like Fiddler ( http://www.telerik.com/fiddler ) may help.

And thanks for helping me find where the Content-Type header was buried in the HttpResponseMessage object!

HttpClient intentionally does not expose charset. Precisely, it can not. It is asynchronous, so when it connects to a server, it waits until response. It is not aware of charset or anything else except TransferEncoding in HttpResponseMessage which does not contain anything except "chunk" or "zip".

So to get an encoding of the response body, we should read it to a variable and then look thoroughly.

Since the Content-Type can be an array of types, you may want to check any of them for validity, but this is assuming server was written correctly and does not intermingle types and char sets

var isJson = response.Content.Headers.GetValues("Content-Type").Any(x=>x.Contains("json"));

var isCharsetUTF8 = response.Content.Headers.GetValues("Content-Type").Any(x=>x.Contains("charset=UTF-8"));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM