如何通过Cookie来HtmlAgilityPack或Web客户端?(How to pass coo

2019-07-20 22:50发布

我使用此代码登录:

CookieCollection cookies = new CookieCollection();
HttpWebRequest request = (HttpWebRequest)WebRequest.Create("example.com");
request.CookieContainer = new CookieContainer();
request.CookieContainer.Add(cookies);
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
cookies = response.Cookies;

string getUrl = "example.com";
string postData = String.Format("my parameters");
HttpWebRequest getRequest = (HttpWebRequest)WebRequest.Create(getUrl);
getRequest.CookieContainer = new CookieContainer();
getRequest.CookieContainer.Add(cookies);
getRequest.Method = WebRequestMethods.Http.Post;
getRequest.UserAgent = "Mozilla/5.0 (Windows NT 6.2; WOW64; rv:19.0) Gecko/20100101 Firefox/19.0";
getRequest.AllowWriteStreamBuffering = true;
getRequest.ProtocolVersion = HttpVersion.Version11;
getRequest.AllowAutoRedirect = true;
getRequest.ContentType = "application/x-www-form-urlencoded";

byte[] byteArray = Encoding.ASCII.GetBytes(postData);
getRequest.ContentLength = byteArray.Length;
Stream newStream = getRequest.GetRequestStream();
newStream.Write(byteArray, 0, byteArray.Length);
newStream.Close();

HttpWebResponse getResponse = (HttpWebResponse)getRequest.GetResponse();
using (StreamReader sr = new StreamReader(getResponse.GetResponseStream(), Encoding.GetEncoding("windows-1251")))
{
        doc.LoadHtml(sr.ReadToEnd());
        webBrowser1.DocumentText = doc.DocumentNode.OuterHtml;
}

然后我想用HtmlWeb(HtmlAgilityPack)或Web客户端解析HTML来的HTMLDocument(HtmlAgilityPack)。

我的问题是,当我使用:

WebClient wc = new WebClient();
webBrowser1.DocumentText = wc.DownloadString(site);

要么

doc = web.Load(site);
webBrowser1.DocumentText = doc.DocumentNode.OuterHtml;

登录消失,所以我觉得我必须以某种方式通过饼干..有什么建议?

Answer 1:

检查HtmlAgilityPack.HtmlDocument饼干

这里是你要找的东西(语法不是100%测试,我只是修改了一些类我通常使用)的例子:

public class MyWebClient
{
    //The cookies will be here.
    private CookieContainer _cookies = new CookieContainer();

    //In case you need to clear the cookies
    public void ClearCookies() {
        _cookies = new CookieContainer();
    }

    public HtmlDocument GetPage(string url) {
        HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
        request.Method = "GET";

        //Set more parameters here...
        //...

        //This is the important part.
        request.CookieContainer = _cookies;

        HttpWebResponse response = (HttpWebResponse)request.GetResponse();
        var stream = response.GetResponseStream();

        //When you get the response from the website, the cookies will be stored
        //automatically in "_cookies".

        using (var reader = new StreamReader(stream)) {
            string html = reader.ReadToEnd();
            var doc = new HtmlDocument();
            doc.LoadHtml(html);
            return doc;
        }
    }
}

这里是你如何使用它:

var client = new MyWebClient();
HtmlDocument doc = client.GetPage("http://somepage.com");

//This request will be sent with the cookies obtained from the page
doc = client.GetPage("http://somepage.com/another-page");

注意:如果你也想用POST方法,只需要创建类似的方法GetPagePOST逻辑,重构类,等等。



Answer 2:

这里有一些建议: 使用的CookieContainer使用WebClient类

但是,它可能只是更容易使用,以保持HttpWebRequest ,并设置在该Cookie CookieContainer

  • HttpWebRequest和的CookieContainer
  • http://msdn.microsoft.com/en-us/library/system.net.httpwebrequest.cookiecontainer.aspx

该代码看起来是这样的:

 // Create a HttpWebRequest
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(getUrl);

// Create the cookie container and add a cookie
request.CookieContainer = new CookieContainer();

// Add all the cookies
foreach (Cookie cookie in response.Cookies)
{
    request.CookieContainer.Add(cookie);
}

第二件事是,你不需要重新下载的网站,因为你已经从你的网络响应和你在这里保存它:

HttpWebResponse getResponse = (HttpWebResponse)getRequest.GetResponse();
using (StreamReader sr = new StreamReader(getResponse.GetResponseStream(), Encoding.GetEncoding("windows-1251")))
{
        webBrowser1.DocumentText = doc.DocumentNode.OuterHtml;
}

你应该能够只是采取的HTML和解析它与HTML敏捷性包:

HtmlDocument doc = new HtmlDocument();
doc.LoadHtml(webBrowser1.DocumentText);

这应该这样做... :)



Answer 3:

尝试缓存从以前的响应cookie本地和重新发送它们每个Web请求如下:

private CookieCollection cookieCollection;

...

    parserObject = new HtmlWeb
                {
                    AutoDetectEncoding = true,
                    PreRequest = request =>
                    {
                        if (cookieCollection != null)
                            cookieCollection.Cast<Cookie>()
                                .ForEach(cookie => request.CookieContainer.Add(cookie));
                        return true;
                    },
                    PostResponse = (request, response) => { cookieCollection = response.Cookies; }
                };


文章来源: How to pass cookies to HtmlAgilityPack or WebClient?