0

我找到了一个获取 HttpOnly cookie 的有效解决方案,但是它只返回一个 cookie,而我希望有多个 cookie。

有人可以告诉我我做错了什么吗?

    private const Int32 InternetCookieHttponly = 0x2000;
    [DllImport("wininet.dll", SetLastError = true)]
    public static extern bool InternetGetCookieEx(string pchURL, string pchCookieName, StringBuilder pchCookieData, ref uint pcchCookieData, int dwFlags, IntPtr lpReserved);
    const int INTERNET_COOKIE_HTTPONLY = 0x00002000;

    public static string GetGlobalCookies(string uri)
    {
        uint datasize = 1024;
        StringBuilder cookieData = new StringBuilder((int)datasize);
        if (InternetGetCookieEx(uri, "cookiename", cookieData, ref datasize, INTERNET_COOKIE_HTTPONLY, IntPtr.Zero)
            && cookieData.Length > 0)
        {
            return cookieData.ToString().Replace(';', ',');
        }
        else
        {
            return null;
        }
    }
4

1 回答 1

0

pchCookieName参数是要检索的 cookie 的区分大小写的名称。您正在传递 string "cookiename",因此该函数将仅返回该 cookie。

根据此 MSDN 代码示例null,您可以通过传递给此参数来检索所有 cookie 。

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Runtime.InteropServices; 

namespace MSDN.Samples.ClaimsAuth 
{ 
    /// <summary> 
    /// WinInet.dll wrapper 
    /// </summary> 
    internal static class CookieReader 
    { 
        /// <summary> 
        /// Enables the retrieval of cookies that are marked as "HTTPOnly".  
        /// Do not use this flag if you expose a scriptable interface,  
        /// because this has security implications. It is imperative that  
        /// you use this flag only if you can guarantee that you will never  
        /// expose the cookie to third-party code by way of an  
        /// extensibility mechanism you provide.  
        /// Version:  Requires Internet Explorer 8.0 or later. 
        /// </summary> 
        private const int INTERNET_COOKIE_HTTPONLY = 0x00002000; 

        [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"></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_HTTPONLY, 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(); 
        } 
    } 
} 
于 2015-07-07T17:51:48.350 回答