首页 > 代码库 > WebBrowser设置Cookie

WebBrowser设置Cookie

winform里面经常会用到WebBrowser,这是一个难点就是如何设置cookies,注意,Docment对象是只读的,所以WebBrowser.Docment.cookie也就只有get方法,没有set方法,下面将介绍如何设置cookies

复制代码
public partial class WebBrowserControl : Form
    {
        private String url;

        [DllImport("wininet.dll", CharSet = CharSet.Auto, SetLastError = true)]
        public static extern bool InternetSetCookie(string lpszUrlName, string lbszCookieName, string lpszCookieData);

        public WebBrowserControl(String path)
        {
            this.url = path;
            InitializeComponent();

            // set cookie
            InternetSetCookie(url, "JSESSIONID", Globals.ThisDocument.sessionID);  

            // navigate
            webBrowser.Navigate(url);  
        }
        ...
}
复制代码

WebBrowser设置Cookie