Skip to main content
 首页 » 编程设计

c#之HttpWebRequest 的响应不显示 UTF-8 符号

2024年01月22日25落叶无声

我有从网站获取响应的简单代码,但有一个小问题。我试图从俄罗斯网站和一个我不知道符号的网站获得回复,从其他网站获得正常文本。哪里可能有问题?

回复来自: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(); 

请您参考如下方法:

kinopoisk.ru 被编码为 WINDOWS-1251(您可以在 Content-Type header 中看到它)。

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