0

I have the following code: What do I put as the second argument for GetPage? The second argument should be the previous cookie request of the same url. for example I put google.com as the first argument to get the cookie when I make a get Request, but for the second how do I insert it?

static void Main()
    {
        GetPage("http://google.com/",cookieContainer??);
    }

public class CookieAwareWebClient : WebClient
    {
        public CookieAwareWebClient(CookieContainer container)
        {
            CookieContainer = container;
        }

        public CookieAwareWebClient()
         : this(new CookieContainer())
        { }

        public CookieContainer CookieContainer { get; private set; }

        protected override WebRequest GetWebRequest(Uri address)
        {
            var request = (HttpWebRequest)base.GetWebRequest(address);
            request.CookieContainer = CookieContainer;
            return request;
        }
    }

 public HtmlAgilityPack.HtmlDocument GetPage(string url, CookieContainer CookieContainer)
    {
        Uri absoluteUri = new Uri("http://google.com/");
        var cookies = CookieContainer.GetCookies(absoluteUri);

        HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
        request.CookieContainer = new CookieContainer();
        foreach (Cookie cookie in cookies)
        {
            request.CookieContainer.Add(cookie);
        }
        request.Method = "GET";

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

        using (var reader = new StreamReader(stream))
        {
            string html = reader.ReadToEnd();
            var doc = new HtmlAgilityPack.HtmlDocument();
            doc.LoadHtml(html);
            return doc;
        }
    }
Puzzle
  • 25
  • 2
  • 2
  • 7
  • It's not entirely clear what you are trying to accomplish. Could you explain a bit more? Your code snippet also includes the _CookieAwareWebClient_ class but the class never gets used. There's some duplication of code there, and it seems likely that _GetPage_ should either be part of CookieAwareWebClient or should take a _CookieAwareWebClient_ as an argument instead of a _CookieContainer_. – Dean Goodman Aug 03 '16 at 00:20
  • What I am trying to accomplish is to get the cookie of a website through a webrequest pass it to a htmldocument so that I can later on parse the htmldocument document – Puzzle Aug 03 '16 at 00:50

0 Answers0