Jason_liu

导航

win32API获取Cookie

private const int INTERNET_COOKIE_HTTPONLY = 0x00002000;
private const int INTERNET_COOKIE = 0x2000;    
        [DllImport("wininet.dll", SetLastError = true)]
        private static extern bool InternetGetCookieEx(
        string url,
            string cookieName,
            StringBuilder cookieData,
        ref int size,
            int flags,
            IntPtr pReserved);
    
        /// <summary>
        /// Returns cookie contents as a string
        /// </summary>
        /// <param name="url">http://***.***.****</param>
        /// <returns></returns>
        public static string GetCookie(string url)
        {
                int size = 512;
                StringBuilder sb = new StringBuilder(size);
                if (!InternetGetCookieEx(url, null, sb, ref size, INTERNET_COOKIE, IntPtr.Zero))
                {
                    if (size < 0)
                    {
                        return null;
                    }
                    sb = new StringBuilder(size);
                    if (!InternetGetCookieEx(url, null, sb, ref size, INTERNET_COOKIE_HTTPONLY, IntPtr.Zero))
                    {
                        return null;
                    }
               }
            return sb.ToString();
        }

posted on 2012-12-10 15:51  Jason_liu  阅读(990)  评论(0编辑  收藏  举报