使用htmlagilitypack读取错误的GetResponseStream

时间:2013-09-30 15:01:51

标签: 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 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;
        }
    }

这里是阅读结果

此代码在此之前工作我无法弄清楚为什么不工作

Visual Studio 2012,C#5

http://i.stack.imgur.com/QXw1h.png

enter image description here

http://i.stack.imgur.com/QXw1h.png

0 个答案:

没有答案