Use the webbowser control once to open the webpage and then read the cookie.

Source: Internet
Author: User

Use the webbowser control once to open the webpage and then read the cookie.

Use the wininet. dll component to read the 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 );
Private static string GetCookies (string url)
{
Uint datasize = 1024;
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 ();
}

Note: During cookie Resolution:

If the cookie value contains commas (,), % 2C can be replaced.

Example:

CookieContainer cookieContainer = new CookieContainer ();
String cookieStr = GetCookies ("http://abc.com /");

String [] cookstr = cookieStr. Split (';');
Foreach (string str in cookstr)
{
String [] data = str. Split ('= ');
If (data [1]. Trim (). ToString (). IndexOf (',')! =-1)
{
Data [1] = data [1]. Trim (). ToString (). Replace (",", "% 2C ");
}
Cookie ck = new Cookie (data [0]. Trim (). ToString (), data [1]. Trim (). ToString ());
CookieContainer. Add (new Uri ("http://abc.com/"), ck );
}

 

End.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.