我使用转换一些代码HttpWebRequest来HttpClient.我遇到的一个问题是从内容类型响应头获取charset.
使用时HttpWebRequest,charset暴露在HttpWebResponse.CharacterSet属性中,就像这样
using (WebResponse response = await this.webRequest.GetResponseAsync())
{
string characterSet = ((HttpWebResponse)response).CharacterSet;
Run Code Online (Sandbox Code Playgroud)
您也可以从WebResponse.ContentType属性或内容类型标题中获取它HttpWebResponse.Headers.
使用时HttpClient,ContentType标题中似乎缺少字符集.
这是我正在使用的代码HttpClient:
using (HttpClient httpClient = new HttpClient(httpClientHandler))
{
using (HttpResponseMessage httpResponseMessage = await httpClient.GetAsync(uri, HttpCompletionOption.ResponseContentRead))
{
charset = httpResponseMessage.Content.Headers.ContentType.CharSet;
Run Code Online (Sandbox Code Playgroud)
CharSet属性始终是null. HttpResponseMessage有一个Headers属性,但它不包含content-type标头. HttpResponseMessage.Content还有一个Headers属性,它似乎包含内容类型标题,但该标题显示"Content-Type: text/html"- 它没有charset部分.
HttpWebResponse对于相同的url 使用第一种方法,我得到Content-Type标头的charset部分.我错过了什么吗?