c# WebRequest using WebBrowser cookie

前端 未结 3 1154
失恋的感觉
失恋的感觉 2020-12-03 12:17

I am logging into a site using a WebBrowser, then i want use regex to get some data , but webRequest didnt use web Browse cookie ,

my webBrowser is in public , is

3条回答
  •  悲哀的现实
    2020-12-03 12:29

        public CookieContainer GetCookieContainer()
        {
            CookieContainer container = new CookieContainer();
    
            foreach (string cookie in webBrowser1.Document.Cookie.Split(';'))
            {
                string name = cookie.Split('=')[0];
                string value = cookie.Substring(name.Length + 1);
                string path = "/";
                string domain = ".google.com"; //change to your domain name
                container.Add(new Cookie(name.Trim(), value.Trim(), path, domain));
            }
    
            return container;
        }
    

    This will work on most sites, however sites that use subdomains might be a problem.

提交回复
热议问题