代码:
http://www.cnblogs.com/hsapphire/archive/2010/09/10/1823384.html
http://blog.csdn.net/attilax/article/details/8595036
[DllImport("wininet.dll", CharSet = CharSet.Auto, SetLastError = true)] static extern bool InternetGetCookieEx(string pchURL, string pchCookieName, StringBuilder pchCookieData, ref System.UInt32 pcchCookieData, int dwFlags, IntPtr lpReserved); [DllImport("wininet.dll", CharSet = CharSet.Auto, SetLastError = true)] static extern int InternetSetCookieEx(string lpszURL, string lpszCookieName, string lpszCookieData, int dwFlags, IntPtr dwReserved);
WebBrowser browser = (WebBrowser)sender; HtmlDocument doc = browser.Document; this.Text = doc.Title; string url = doc.Url.AbsoluteUri; //string cookies2 = GetCookies(url); //保留cookie // cookieString = doc.Cookie; cookieString = GetCookies(url);
private static string GetCookies(string url) { uint datasize = 256; StringBuilder cookieData = new StringBuilder((int)datasize); if (!InternetGetCookieEx(url, null, cookieData, ref datasize, 0x2000, IntPtr.Zero)) { if (datasize < 0) return null; cookieData = new StringBuilder((int)datasize); if (!InternetGetCookieEx(url, null, cookieData, ref datasize, 0x00002000, IntPtr.Zero)) return null; } return cookieData.ToString(); }