Implementation method of obtaining cookie from C based on WebBrowser
- 2021-08-12 03:24:31
- OfStack
In this paper, the implementation method of C # obtaining cookie based on WebBrowser is described with an example. Share it for your reference, as follows:
private void BtnOpenUrl_Click(object sender, EventArgs e)
{
if (txtUrl.Text != "")
{
MywebBrowser.Url = new Uri(txtUrl.Text);
}
}
private void BtnGetCookie_Click(object sender, EventArgs e)
{
CookieContainer myCookieContainer = new CookieContainer();
if (MywebBrowser.Document.Cookie != null)
{
string cookieStr = MywebBrowser.Document.Cookie;
string[] cookstr = cookieStr.Split(';');
foreach (string str in cookstr)
{
string[] cookieNameValue = str.Split('=');
Cookie ck = new Cookie(cookieNameValue[0].Trim().ToString(), cookieNameValue[1].Trim().ToString());
ck.Domain = "www.google.com";
myCookieContainer.Add(ck);
}
}
}
I hope this article is helpful to everyone's C # programming.