C# webBrowser 获取cookies 设置cookies
[DllImport("wininet.dll", CharSet = CharSet.Auto, SetLastError = true)] internal 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)] internal static extern int InternetSetCookieEx(string lpszURL, string lpszCookieName, string lpszCookieData, int dwFlags, IntPtr dwReserved); public 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(); }
//获取cookies private void WebBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e) { if (webBrowser1.DocumentText.Contains(str)) { string cookies = GetCookies(url); } }
//设置cookies InternetSetCookie(url, null, new Cookie(HttpUtility.UrlEncode("JSESSIONID"), JSESSIONID, "", "/").ToString());
评论前必须登录!
注册