WebBrowser Cookie Operation

Source: Internet
Author: User
Tags foreach set cookie trim

WebBrowser Cookie Operation

1. Obtaining Cookies in WebBrowser

Cookiecontainer Mycookiecontainer = new Cookiecontainer ();

string cookiestr = WebBrowser1.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);
}

WebClient setting cookie!
WebClient WC = new WebClient ();
Wc. Headers.add ("Cookie", "phpsessid=" + cookie + ";");
Note that this is a Cookie, not a set-cookie.
byte[] re = WC. Uploaddata (Global.rootpath + "test.php", new byte[0]);
System.Text.UTF8Encoding converter = new System.Text.UTF8Encoding ();
String str = converter. GetString (re);


2. Setting a cookie in WebBrowser
public partial class Webbrowsercontrol:form
{
Private String URL;

[DllImport ("Wininet.dll", CharSet = CharSet.Auto, SetLastError = True)]
public static extern bool InternetSetCookie (string lpszurlname, String lbszcookiename, string lpszcookiedata);

Public Webbrowsercontrol (String path)
{
This.url = path;
InitializeComponent ();

Set Cookie
InternetSetCookie (URL, "Jsessionid", Globals.ThisDocument.sessionID);

Navigate
Webbrowser.navigate (URL);
}
}

3. Pass the WebBrowser cookie information to HttpWebRequest

Build a "cookiecontainer" to keep the cookies in the WebBrowser.

Login cookie in WebBrowser saved in WebBrowser.Document.Cookie
Cookiecontainer Mycookiecontainer = new Cookiecontainer ();

The cookie of String is to be converted into a cookie type and put into Cookiecontainer
string cookiestr = WebBrowser1.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.abc.com";//Must be written to
    mycookiecontainer.add (CK);
}

HttpWebRequest Hreq = (HttpWebRequest) httpwebrequest.create ("http://www.abc.com/search.asp");
Hreq. Method = "POST";
Hreq. ContentType = "application/x-www-form-urlencoded";

Self-created Cookiecontainer
Hreq. Cookiecontainer = Mycookiecontainer;

String postdata = "Id=2005&action=search&name=";
byte[] byte1 = Encoding.ASCII.GetBytes (postdata);
Hreq. ContentLength = byte1. Length;

Stream Poststream = hreq. GetRequestStream ();
Poststream. Write (byte1, 0, Byte1. Length);
Poststream. Close ();

HttpWebResponse hres = (httpwebresponse) hreq. GetResponse ();

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.