简体   繁体   English

Content-Type 字符集是否未从 HttpResponseMessage 公开?

[英]Is the Content-Type charset not exposed from HttpResponseMessage?

I'm converting some code from using HttpWebRequest to HttpClient .我正在将一些代码从使用HttpWebRequest转换为HttpClient One problem I'm having is getting the charset from the content-type response header.我遇到的一个问题是从内容类型响应 header 获取字符集。

When using HttpWebRequest , the charset is exposed in the HttpWebResponse.CharacterSet property, like this使用HttpWebRequest时,字符集在HttpWebResponse.CharacterSet属性中公开,如下所示

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 .您还可以从WebResponse.ContentType属性或从HttpWebResponse.Headers中的内容类型 header 获取它。

Using HttpClient , the charset seems to be missing from the ContentType header.使用HttpClient时, ContentType header 中似乎缺少字符集。

Here's the code that I'm using for HttpClient :这是我用于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 . CharSet 属性始终为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. HttpResponseMessage有一个Headers属性,但它不包含内容类型HttpResponseMessage.Content也有一个 Headers 属性,它似乎包含内容类型 header,但 header 显示"Content-Type: text/html" - 它没有字符集部分。

Using the first approach with HttpWebResponse for the same url, I get the charset portion of the Content-Type header. Am I missing something?对同一个 url 使用HttpWebResponse的第一种方法,我得到了 Content-Type header 的字符集部分。我错过了什么吗?

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 中发出字符集,因为你的问题是谷歌上的第一个问题,我在下面几页找到了答案,这里是代码

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.我相信从服务器返回的Content-Type标头必须包含像'text/html;charset=UTF-8'这样的'text/html;charset=UTF-8' ,以便它显示在CharSet属性中。 Checking the raw response in a tool like Fiddler ( http://www.telerik.com/fiddler ) may help.在像 Fiddler ( http://www.telerik.com/fiddler ) 这样的工具中检查原始响应可能会有所帮助。

And thanks for helping me find where the Content-Type header was buried in the HttpResponseMessage object!感谢您帮助我找到HttpResponseMessage对象中Content-Type标头的位置!

HttpClient intentionally does not expose charset. HttpClient 有意不公开字符集。 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".它不知道字符集或除 HttpResponseMessage 中的 TransferEncoding 之外的任何其他内容,它不包含除“chunk”或“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由于 Content-Type 可以是类型数组,您可能想检查其中任何一个的有效性,但这是假设服务器编写正确并且不会混合类型和字符集

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"));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 无法在 HttpResponseMessage 标头上设置 Content-Type 标头? - Can't set Content-Type header on HttpResponseMessage headers? 如何从 HttpResponseMessage 读取应用程序/pdf 内容类型并将其转换为 stream - How to read application/pdf content type from HttpResponseMessage and convert it into stream API Get 需要 Content-Type application/json;charset=UTF-8 - Http 客户端问题 - API Get requires Content-Type application/json;charset=UTF-8 - Issue with Http Client 从Content-Type获取扩展 - Get extension from Content-Type 从 HttpActionContext 获取内容类型 - Getting content-type from HttpActionContext 更改WCF服务以接受内容类型“ application / xml; charset = utf-8” - Change WCF service to accept content-type “application/xml;charset=utf-8” 西里尔符号显示为问号。 内容类型:文本/xml;字符集=windows-1251; - Cyrillic symbols show as question marks. Content-Type:text/xml;charset=windows-1251; 从 HttpResponseMessage 获取内容/消息 - Getting content/message from HttpResponseMessage HttpResponseMessage 内容未返回正确的内容类型 - HttpResponseMessage Content not returning the correct content type 返回HttpResponseMessage的控制器未正确设置内容类型 - Controller returning HttpResponseMessage not setting content type correctly
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM