无法使用 HttpWebRequest/HttpWebResponse 下载 html 字符串
本文关键字:HttpWebResponse 下载 html 字符串 HttpWebRequest | 更新日期: 2023-09-27 17:57:01
i 使用 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
通过使用webrequest.Headers.Add("Accept-Encoding", "gzip, deflate");
,您告诉服务器您理解压缩响应。删除该标头并使用正常的 UTF8 编码,而不是您正在使用的 1252。然后,您应该获得正确的字符串。你可以只使用System.Text.Encoding.UTF8
.