我使用HttpWebRequest/HttpWebResponse获取HTML文档,以下代码已经运行,但我无法将接收到的流编码为HTML字符串:
因此,我使用以下代码行来测试是否成功请求。
屏幕上的结果是OK的,这意味着请求已经发送,但屏幕上公布的响应字符串不是HTML格式,而是一些奇怪的东西,像这样:@32u%&$(@*#Eeeuw
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);
屏幕上的结果是OK的,这意味着请求已经发送,但屏幕上公布的响应字符串不是HTML格式,而是一些奇怪的东西,像这样:@32u%&$(@*#Eeeuw