【问题标题】:HttpWebRequest's response don't show UTF-8 symbolsHttpWebRequest 的响应不显示 UTF-8 符号
【发布时间】:2012-06-12 19:14:05
【问题描述】:

我有从网站获取响应的简单代码,但有一个小问题。我正在尝试从俄罗斯网站和一个网站获得未知符号的响应,而从另一个网站获得正常文本。哪里可能有问题?

回复来自:www.kinopoisk.ru

������ �����...

回复来自:www.yandex.ru

Греция - Чехия。 1:2...

    HttpWebRequest http = (HttpWebRequest) HttpWebRequest.Create("http://");
    http.Timeout = 30000;
    http.KeepAlive = true;
    http.ContentType = "application/x-www-form-urlencoded";
    http.UserAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:12.0) Gecko/20100101 Firefox/12.0";
    http.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8";
    http.Proxy = null;

    WebResponse response = http.GetResponse();
    Stream istream = response.GetResponseStream();
    StreamReader reader = new StreamReader(istream);

    Response.Write(reader.ReadToEnd());

    reader.Close();

【问题讨论】:

    标签: c# utf-8 httpwebrequest utf8-decode


    【解决方案1】:

    kinopoisk.ru 被编码为WINDOWS-1251(您可以在Content-Type 标头中看到这一点)。

    您需要将 Encoding.GetEncoding(1251) 传递给 StreamReader 以对其进行解码。

    【讨论】:

      【解决方案2】:

      这是一个字符集问题。如果要获取请求响应的字符集,HttpWebResponse 类为我们提供了一个名为 CharacterSet 的属性。该属性返回字符串类型值。

      myWebRequest = (HttpWebRequest)HttpWebRequest.Create(url);
      myWebRequest.Method = "GET";
      myWebResponse = (HttpWebResponse)myWebRequest.GetResponse();
      string str = myWebResponse.CharacterSet;
      

      如果您想知道使用哪种编码方法对响应进行编码,为此我们有一个名为 ContentEncoding 的 HttpWebRequest 类的属性。该属性返回字符串值。

      myWebRequest = (HttpWebRequest)HttpWebRequest.Create(url);
      myWebRequest.Method = "GET";
      myWebResponse = (HttpWebResponse)myWebRequest.GetResponse();
      string str = myWebResponse.ContentEncoding;
      

      【讨论】:

        猜你喜欢
        • 2011-07-17
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-09-19
        • 2021-12-28
        • 1970-01-01
        • 2015-11-15
        • 2012-01-11
        相关资源
        最近更新 更多