可以让WebBrowser控件与HttpWebRequest共享cookie吗?

发布于 2024-11-24 01:31:13 字数 271 浏览 4 评论 0原文

有没有办法让C#.NET中的WebBrowser控件和HttpWebRequest发出的请求共享cookie?

例如,如果使用 HttpWebRequest 以编程方式发出请求,然后 HttpWebResponse 设置 cookie,是否有办法确保这也在 WebBrowser 控件中设置?

同样,如果用户使用 WebBrowser 控件进行导航并设置了 cookie,是否有办法确保 HttpWebRequest 的 CookieContainer 也被更新?

感谢您的帮助!

is there a way to make the WebBrowser control in C#.NET and requests made by HttpWebRequest share cookies?

E.g. if a request is made programmatically with HttpWebRequest and then the HttpWebResponse sets a cookie, is there a way to make sure this is also set in the WebBrowser control?

and likewise, if the user navigates with the WebBrowser control and a cookie is set, is there a way to ensure the CookieContainer for the HttpWebRequest is also updated?

Thanks for any help!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

耳根太软 2024-12-01 01:31:13

您需要使用 InternetSetCookieEx / InternetGetCookieEx API 手动同步 cookie,这需要您知道相关页面使用的所有子下载的所有 URL。

您还需要传递 INTERNET_COOKIE_HTTPONLY 标志,以确保您的应用程序可以看到 HTTPONLY cookie。

You'd need to synchronize the cookies manually using the InternetSetCookieEx / InternetGetCookieEx APIs, and this would require that you know all of the URLs of all of the subdownloads used by the page in question.

You also need to pass the INTERNET_COOKIE_HTTPONLY flag to ensure that HTTPONLY cookies are seen by your application.

梦中的蝴蝶 2024-12-01 01:31:13
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(myUri);
request.CookieContainer = new CookieContainer();
request.CookieContainer.SetCookies(myUri, webBrowser1.Document.Cookie);

来源

反之亦然(我不确定):

HttpWebRequest request = (HttpWebRequest)WebRequest.Create(myUri);
//request.CookieContainer = new CookieContainer();
request.GetResponse();
webBrowser1.Document.Cookie = request.CookieContainer.GetCookies(myUri);
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(myUri);
request.CookieContainer = new CookieContainer();
request.CookieContainer.SetCookies(myUri, webBrowser1.Document.Cookie);

(source)

And vice versa (I'm not sure):

HttpWebRequest request = (HttpWebRequest)WebRequest.Create(myUri);
//request.CookieContainer = new CookieContainer();
request.GetResponse();
webBrowser1.Document.Cookie = request.CookieContainer.GetCookies(myUri);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文