我正在使用 WebBrowser 的导航功能加载网站,我希望浏览器使用我提供的 cookie 加载页面。
以下代码无效:
wb.Navigate(url, null, null, "Cookie: " + cookie + "\n");
我做错了什么?我必须使用 InternetSetCookie 吗? ?这似乎不是最佳解决方案。
最佳答案
看起来有更好的方法:
导入InternetSetCookie
函数:
[DllImport("wininet.dll", CharSet = CharSet.Auto, SetLastError = true)]
static extern bool InternetSetCookie(string lpszUrlName, string lpszCookieName, string lpszCookieData);
创建 Cookie
对象:
Cookie temp1 = new Cookie("KEY1", "VALUE1", "/Path/To/My/App", "/");
调用InternetSetCookie
函数为该URL设置cookie
InternetSetCookie("https://my.url.com/Path/To/My/App", null, temp1.ToString() + "; expires = Sun, 01-Jan-2013 00:00:00 GMT");
导航
WebBrowser
到您要访问的 URL。
webBrowser1.Navigate("https://my.url.com/Path/To/My/App");
认为这是该问题的最佳解决方案:)。
关于c# - 在 WebBrowser 控件中设置 cookie,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2830586/