HttpWebResponse 的编码问题

发布于 2024-12-19 03:49:11 字数 1020 浏览 0 评论 0原文

我从 http Web 响应接收的字符编码有问题,我收到 ? 而不是 é

我根据网页的 Content-Type 设置编码,即 text/javascript;字符集=ISO-8859

我的代码是:

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

你能告诉我它出了什么问题吗?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

彼岸花似海 2024-12-26 03:49:11

在提出请求之前尝试添加以下内容:

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

顺便说一句,如果您想尝试我建议的解决方案,您应该将 StreamReader 保留为 ISO-8859-1(而不是 UTF8)。祝你好运!

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!

唔猫 2024-12-26 03:49:11

您是否尝试过将其设置为UTF-8?
此外,您还发送了一个引荐来源网址,我认为您尝试设置 UserAgent。下面的代码与您的代码相同,但不会遍历字节数组并设置 useragent 和 utf8 编码。

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();
}

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();
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文