C# GetResponseStream读取HtmlAlityPack错误

C# GetResponseStream读取HtmlAlityPack错误,c#,httpwebrequest,html-agility-pack,streamreader,getresponsestream,C#,Httpwebrequest,Html Agility Pack,Streamreader,Getresponsestream,好的,这就是整个函数 但是读取的值非常不正确 我不知道有什么问题 编辑 好的,看起来问题是关于gzip压缩。 如何解压缩GetResponseStream public static List<object> func_DoHTTPWebRequest(PerVotingSite myPerVote, string srUrl, string srCookiePrev = "", string srRefererParameter = null, string

好的,这就是整个函数 但是读取的值非常不正确 我不知道有什么问题

编辑 好的,看起来问题是关于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;
        }
    }
public static List func_DoHTTPWebRequest(PerVotingSite myPerVote,string srUrl,string srCookiePrev=“”,string srRefererParameter=null,
字符串srBrowserAgent=“Mozilla/5.0(Windows NT 6.1;WOW64)AppleWebKit/537.31(KHTML,类似Gecko)Chrome/26.0.1410.64 Safari/537.31”)
{
尝试
{
srUrl=”http://stackoverflow.com/“;//用于测试目的
字符串srReferer=myPerVote.srReferer;
if(srRefererParameter!=null)
srReferer=srReferer参数;
bool blKeepAlive=myPerVote.blKeepAlive;
int irRequestTimeOut=myPerVote.irrequestimeout;
如果(irRequestTimeOut==0)
irRequestTimeOut=OtomatikVoter.irTimeOut;
bool blKeepCookies=myPerVote.blKeepCookies;
HttpWebRequest hWebReq=(HttpWebRequest)WebRequest.Create(srUrl);
hWebReq.KeepAlive=blKeepAlive;
hWebReq.Referer=srReferer;
hWebReq.Timeout=不可请求超时;
hWebReq.ReadWriteTimeout=不请求超时;
hWebReq.UserAgent=srBrowserAgent;
hWebReq.Accept=“text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8”;
WebHeaderCollection myWebHeaderCollection=hWebReq.Headers;
添加(“接受语言”,“en gb,en;q=0.5”);
添加(“接受编码”、“gzip、deflate”);
如果(srCookiePrev.Length>1)
添加(“Cookie”,srCookiePrev);
字符串srCookie=“”;
HtmlAgilityPack.HtmlDocument hDoc=新的HtmlAgilityPack.HtmlDocument();
使用(HttpWebResponse hWebResp=(HttpWebResponse)hWebReq.GetResponse())
{
使用(var resultStream=hWebResp.GetResponseStream())
{
if(hWebResp.Headers[“Set Cookie”!=null&&blKeepCookies==true)
srCookie=hWebResp.Headers[“Set Cookie”].ToString();
加载(resultStream,Encoding.UTF8);
}
}
返回新列表{hDoc,srCookie,hWebReq};
}
捕获(例外E)
{
writeError(E,“func_DoHTTPWebRequest”);
返回null;
}
}
这里是读取结果

这个代码以前是有效的,现在我不明白为什么不起作用

Visual Studio 2012,C#5


这似乎与Html敏捷包无关。确切的错误是什么(stacktrace)?实际上我发现了错误。它是关于gzip和解压缩的:D