Is Content-Type encoding inaccessible from HttpResponseMessage?

I am converting code from HttpWebRequest to HttpClient . One of the problems I ran into is getting the encoding from the response header of the content type.

When using HttpWebRequest encoding is displayed in the HttpWebResponse.CharacterSet property, for example,

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

You can also access it from the WebResponse.ContentType property or from the content header in HttpWebResponse.Headers .

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

Here is the code I use 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 does not contain a content header. HttpResponseMessage.Content also has a Headers property, which appears to contain a content type header, but this header shows "Content-Type: text/html" - it does not have a charset part.

Using the first approach with HttpWebResponse for the same URL, I get the encoding part of the Content-Type header. Did I miss something?

+10
source share
4 answers

I was looking for a charset inside HttpResponseMessage, and since your question is the first in google and that I found the answer a few 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()); 
+4
source

I believe that the Content-Type header returned from the server should contain β€œcharset”, for example 'text/html;charset=UTF-8' so that it appears in the CharSet property. Checking the original 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!

+1
source

HttpClient intentionally does not disclose the encoding. It certainly cannot. It is asynchronous, so when it connects to the server, it waits for a response. He does not know about encoding or anything else except TransferEncoding in HttpResponseMessage, which contains nothing but "chunk" or "zip".

So, in order to get the encoding of the response body, we need to read its variable, and then carefully study it.

0
source

You can get it like this:

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

Source: https://habr.com/ru/post/954900/


All Articles