我使用HttpWebRequest / HttpWebResponse获取html文档,代码跟随运行但我无法将收到的流编码为html字符串:
string uri = "https://myfavoritesite.come";
HttpWebRequest webrequest = (HttpWebRequest)WebRequest.Create(uri);
webrequest.KeepAlive = true;
webrequest.Method = "GET";
webrequest.ContentType = "text/html";
webrequest.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8";
//webrequest.Connection = "keep-alive";
webrequest.Host = "cat.sabresonicweb.com";
webrequest.Headers.Add("Accept-Encoding", "gzip, deflate");
webrequest.Headers.Add("Accept-Language", "en-US,en;q=0.5");
webrequest.UserAgent = "Mozilla/5.0 (Windows NT 6.1; rv:18.0) Gecko/20100101 Firefox/18.0";
HttpWebResponse webresponse = (HttpWebResponse)webrequest.GetResponse();
Console.Write(webresponse.StatusCode);
Stream receiveStream = webresponse.GetResponseStream();
Encoding enc = System.Text.Encoding.GetEncoding(1252);//1252
StreamReader loResponseStream = new
StreamReader(receiveStream, enc);
string Response = loResponseStream.ReadToEnd();
loResponseStream.Close();
webresponse.Close();
Console.Write(Response);
所以,我使用下面的代码行来测试是否有成功的请求。
Console.Write(webresponse.StatusCode);
屏幕上的结果还可以,这意味着发送了请求,但屏幕上显示的响应字符串不是html格式,这是如此奇怪:@ 32u%& $ & ;(@ *#Eeeuw
答案 0 :(得分:2)
通过使用webrequest.Headers.Add("Accept-Encoding", "gzip, deflate");
,您告诉服务器您了解压缩响应。删除该标头并使用正常的UTF8编码而不是您正在使用的1252。然后你应该得到正确的字符串。您可以使用System.Text.Encoding.UTF8
。