8

WebBrowser コントロールが使用する Cookie を読み書きする方法はありますか?

私はこのようなことをしています...

string resultHtml;
HttpWebRequest request = CreateMyHttpWebRequest(); // fills http headers and stuff
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
using (StreamReader sr = new StreamReader(response.GetResponseStream()))
{
    resultHtml = sr.ReadToEnd();
}
WebBrowser browser = new WebBrowser();
browser.CookieContainer = request.CookieContainer; // i wish i could do this :(
browser.NavigateToString(resultHtml);  
4

6 に答える 6

7

コントロールとCookieについて混乱を招く可能性があることの1つは、WebBrowser一見すると、アプリが個別のCookieストアを取得しているように見えることが多いことです。たとえば、永続的なCookieを保存するサイトにログインしてユーザーを特定する場合、コントロールをホストしているアプリ内からそのサイトにログインしているように見えるかどうかは、InternetExplorer経由でログインしているように見えるかどうかとは無関係です。 。

実際、異なるIDでログインすることもできます。

However, although it might be natural to draw the conclusion that each app hosting the WebBrowser therefore gets its own cookies, in fact that's not true. There are merely two sets of cookies: the ones used in 'low integrity' mode (which is what IE runs in by default), and the other set, which is what you'll get in a normal app that hosts the WebBrowser and also what you'll get if you run IE elevated.

于 2012-06-28T14:54:51.470 に答える
6

Web ブラウザー コントロールは、ネットワークに WinInet を使用します。具体的には、Cookie の管理に InternetSetCookie(Ex) および InternetGetCookie(Ex) 関数を使用します。.Net には WinInet ラッパーはありませんが、p-invoke は可能です。

于 2009-12-29T19:46:31.180 に答える
0

数日前に同じ問題に直面しました。前の回答の例に加えて、WebBrowser コントロールの Win32 ラッパーを次に示します。この実装の利点は、既定の WebBrowser コントロールよりも多くのオプションを公開することです。

残念ながら WPF ネイティブではないため、WPF で使用する場合はラッパーを作成する必要があります。

http://code.google.com/p/csexwb2/

于 2011-06-22T21:17:09.930 に答える