好的,这就是整个函数但是读取的值非常不正确我不知道可能是什么问题
编辑好吧,问题似乎与 gzip 压缩有关。如何解压缩 GetResponseStream ?
public static List<object> func_DoHTTPWebRequest(PerVotingSite myPerVote, string srUrl, string srCookiePrev = "", string srRefererParameter = null,
string srBrowserAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.31 (KHTML, like Gecko) Chrome/26.0.1410.64 Safari/537.31")
{
try
{
srUrl = "http://stackoverflow.com/"; // for testing purposes
string srReferer = myPerVote.srReferer;
if (srRefererParameter != null)
srReferer = srRefererParameter;
bool blKeepAlive = myPerVote.blKeepAlive;
int irRequestTimeOut = myPerVote.irRequestsTimeOut;
if (irRequestTimeOut == 0)
irRequestTimeOut = OtomatikVoter.irTimeOut;
bool blKeepCookies = myPerVote.blKeepCookies;
HttpWebRequest hWebReq = (HttpWebRequest)WebRequest.Create(srUrl);
hWebReq.KeepAlive = blKeepAlive;
hWebReq.Referer = srReferer;
hWebReq.Timeout = irRequestTimeOut;
hWebReq.ReadWriteTimeout = irRequestTimeOut;
hWebReq.UserAgent = srBrowserAgent;
hWebReq.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8";
WebHeaderCollection myWebHeaderCollection = hWebReq.Headers;
myWebHeaderCollection.Add("Accept-Language", "en-gb,en;q=0.5");
myWebHeaderCollection.Add("Accept-Encoding", "gzip, deflate");
if (srCookiePrev.Length > 1)
myWebHeaderCollection.Add("Cookie", srCookiePrev);
string srCookie = "";
HtmlAgilityPack.HtmlDocument hDoc = new HtmlAgilityPack.HtmlDocument();
using (HttpWebResponse hWebResp = (HttpWebResponse)hWebReq.GetResponse())
{
using (var resultStream = hWebResp.GetResponseStream())
{
if (hWebResp.Headers["Set-Cookie"] != null && blKeepCookies == true)
srCookie = hWebResp.Headers["Set-Cookie"].ToString();
hDoc.Load(resultStream,Encoding.UTF8);
}
}
return new List<object> { hDoc, srCookie, hWebReq };
}
catch (Exception E)
{
SpecialFunctions.writeError(E, "func_DoHTTPWebRequest");
return null;
}
}
这里是读取结果
这段代码以前可以工作,现在我不知道为什么不工作
视觉工作室 2012 , C# 5