Jack
Jack

Reputation: 16724

encoding problems with HttpWebResponse

I have problems with characters encoding received from http web response, I receive ? instead é.

I set the encoding to according Content-Type of web page that's text/javascript; charset=ISO-8859;

My code is:

HttpWebRequest request = (HttpWebRequest)WebRequest.Create(..);
request.Method = "GET";
request.AllowAutoRedirect = false;
request.Referer = "Mozilla/5.0 (Windows NT 6.1; rv:7.0.1) Gecko/20100101 Firefox/7.0.1";
request.Headers.Add("DNT", "1");
request.Accept = "text/html,application/xhtml+xml,application/xml";

HttpWebResponse response = (HttpWebResponse)request.GetResponse();
Stream stream = response.GetResponseStream();
StreamReader sr = new StreamReader(stream, Encoding.GetEncoding("iso-8859-1"));

char[] buf = new char[256];
int count;
StringBuilder buffer = new StringBuilder();

while ((count = sr.Read(buf, 0, 256)) > 0)
{
    buffer.Append(buf, 0, count);
}

string responseStr = buffer.ToString();
Console.WriteLine(responseStr);
response.Close();
stream.Close();
sr.Close();

Can you tell me what is wrong with it?

Upvotes: 1

Views: 6053

Answers (2)

Henk J Meulekamp
Henk J Meulekamp

Reputation: 3057

Have you tried setting it at UTF-8? Further more you send a referrer which I think you tried to set the UserAgent. The code below is the same as yours, but then does not go over the byte array and sets the useragent and utf8 encoding.

var request = (HttpWebRequest)WebRequest.Create(url);
request.Method = "GET";
request.AllowAutoRedirect = false;
request.UserAgent = "Mozilla/5.0 (Windows NT 6.1; rv:7.0.1) Gecko/20100101 Firefox/7.0.1";
request.Headers.Add("DNT", "1");
request.Accept = "text/html,application/xhtml+xml,application/xml";

using(var response = (HttpWebResponse)request.GetResponse())
using(var stream = response.GetResponseStream())
using (var sr = new StreamReader(stream, Encoding.UTF8))
{
    string responseStr = sr.ReadToEnd();
    Console.WriteLine(responseStr);
    response.Close();
    if (stream != null)
        stream.Close();
    sr.Close();
}

Upvotes: 1

Tung
Tung

Reputation: 5444

Try adding the following before you make your request:

request.Headers.Add(HttpRequestHeader.AcceptCharset, "ISO-8859-1");

Btw, you should keep your StreamReader with ISO-8859-1 (instead of UTF8) if you want to try my proposed solution. Good luck!

Upvotes: 2

Related Questions