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);  
        }
        ...
}

 

posted @ 2014-12-02 14:47  Napoléon  阅读(5986)  评论(0编辑  收藏  举报