c# - 在 WebBrowser 控件中设置 cookie

标签 c# .net browser webbrowser-control

我正在使用 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/

相关文章:

c# - 是否可以调用类方法,从控制台或 powershell 设置/获取属性

javascript - HTML 单页应用,用浏览器填写用户名和密码

javascript - 捕获浏览器事件

c# - 使用 WinSCard 库获取 mifare 卡序列号

python - 如何禁用 python 中的 webbrowser 消息?

c# - 启用和禁用控件在 asp.net 中无法正常工作

c# - linq to entities dynamic where 从 lambdas 构建

c# - 将数据保存在 Form2 中,帐户 ID 位于 1 列中,以查看谁拥有该数据

c# - 我可以使用响应式(Reactive)扩展来对 Windows 服务的调用进行排队吗?

.net - ResolveEventArgs.RequestingAssembly 为 Null