How to pass cookies to HtmlAgilityPack or WebClient?

后端 未结 3 1704
一向
一向 2020-12-17 20:02

I use this code to login:

CookieCollection cookies = new CookieCollection();
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(\"example.com\");
req         


        
3条回答
  •  眼角桃花
    2020-12-17 20:34

    There are some recommendations here: Using CookieContainer with WebClient class

    However, it's probably just easier to keep using the HttpWebRequest and set the cookie in the CookieContainer:

    • HTTPWebRequest and CookieContainer
    • http://msdn.microsoft.com/en-us/library/system.net.httpwebrequest.cookiecontainer.aspx

    The code looks something like this:

     // 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);
    }
    

    The second thing is that you don't need to download the site again, since you already have it from your web response and you're saving it here:

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

    You should be able to just take the HTML and parse it with the HTML Agility Pack:

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

    And that should do it... :)

提交回复
热议问题