欢迎访问!
您的支持是我们不断前行的动力!

C# webBrowser 获取cookies 设置cookies

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());

 

赞(0) 打赏
转载请注明出处,未注明出处不得转载,蜘蛛侠 » C# webBrowser 获取cookies 设置cookies
分享到: 更多 (0)

评论 抢沙发

评论前必须登录!

 

蜘蛛侠,您身边的爬虫专家!

觉得文章有用就打赏一下文章作者吧

支付宝扫一扫打赏

微信扫一扫打赏