Veverke
Veverke

Reputation: 11338

How to set Content-Type header in a http get response using C#'s HttpClient?

I am having problems in retrieving the contents of a http get request in the proper charset.

I tried several pieces of code, such as the following:

HttpClient h = new HttpClient();
//Content-Type: text/html; charset=UTF-8

//p.s. contents are in hebrew.    
var resp = h.GetAsync("http://www.wnf.co.il");
var content = resp.Result.Content;

//remove the default Content-Type header
content.Headers.Remove("Content-Type");
content.Headers.Add("Content-Type", "text/html; charset=utf-8");
var res = content.ReadAsStringAsync();
var s = res.Result;

Console.WriteLine(s);

which still does not help, I still get the content in wrong encoding.

enter image description here

This post clarifies that setting the header's request headers charset will not help, it's the response's one that needs to be set. (Besides, you will get an error in trying to add header "Content-Type" to a request Header.)

But I still could not end up with working retrieval of the content in the proper charset (utf-8).

What am I missing ?

I have been doing similar stuff with hebrew sites for a while, in comparing the response's header in Fiddler from this site and others where I do not have this problem - the only difference I see is indeed this Content-Type header in the response.

Upvotes: 0

Views: 2607

Answers (1)

Todd Menier
Todd Menier

Reputation: 39289

The issue is probably due to this bug:

https://connect.microsoft.com/VisualStudio/feedback/details/790174/system-net-http-httpcontent-readasstringasync-does-not-handle-imperfect-content-type-headers

The work-around is to get the response as a byte array and encode it yourself:

var bytes = await content.ReadAsByteArrayAsync();
var s = Encoding.UTF8.GetString(bytes, 0, bytes.Length);

As a side-note, is there a reason you're using .Result instead of await? You are blocking the current thread unnecessarily and setting yourself up for deadlocks.

Upvotes: 1

Related Questions