Get browser cookies

In webbowser, if you want to get a cookie, you can use the following method

webBrowser1.Document.Cookie;

However, the cookies obtained by this method are often incomplete.

Need to use the following method

  private const int INTERNET_COOKIE_HTTPONLY = 0x2000;

        public static string GetCookie(string url)         {             int capacity = 0x200;             StringBuilder cookieData = new StringBuilder(capacity);             if (!InternetGetCookieEx(url, null, cookieData, ref capacity, 0x2000, IntPtr.Zero))             {                 if (capacity < 0)                 {                     return null;                 }                 cookieData = new StringBuilder(capacity);                 if (!InternetGetCookieEx(url, null, cookieData, ref capacity, 0x2000, IntPtr.Zero))                 {                     return null;                 }             }             return cookieData.ToString();         }

        [DllImport("wininet.dll", SetLastError = true)]         private static extern bool InternetGetCookieEx(string url, string cookieName, StringBuilder cookieData, ref int size, int flags, IntPtr pReserved);     

 The full URL must be passed in when calling

cookie = CookieReader.GetCookie(this.webBrowser1.Url.ToString());

 

The cookie obtained in this way is relatively complete

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326497915&siteId=291194637