分类 Winform 下的文章

C# winform 中 webborwser 获取cookie不全的解决方法


有时候webborser获取到的cookie不全 使用下面这个方法即可

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

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