C#读取Cookie

 1 public class HttpCookie
 2     {
 3         [DllImport("wininet.dll", CharSet = CharSet.Auto, SetLastError = true)]
 4         static extern bool InternetGetCookieEx(string pchURL, string pchCookieName, StringBuilder pchCookieData, ref System.UInt32 pcchCookieData, int dwFlags, IntPtr lpReserved);
 5         public static string GetCookies(string url)
 6         {
 7             uint datasize = 1024;
 8             StringBuilder cookieData = new StringBuilder((int)datasize);
 9             if (!InternetGetCookieEx(url, null, cookieData, ref datasize, 0x2000, IntPtr.Zero))
10             {
11                 if (datasize < 0)
12                     return null;
13 
14                 cookieData = new StringBuilder((int)datasize);
15                 if (!InternetGetCookieEx(url, null, cookieData, ref datasize, 0x00002000, IntPtr.Zero))
16                     return null;
17             }
18             return cookieData.ToString();
19         }  
20     }

使用此方法可以获得Cookie

posted @ 2017-03-02 19:30  芝麻学问  阅读(443)  评论(0编辑  收藏  举报