1. Set cookie in webbrowser 1 public partial class webbrowsercontrol: Form
2 {
3 private string URL;
4
5 [dllimport ("wininet. dll", charset = charset. Auto, setlasterror = true)]
6 public static extern bool internetsetcookie (string lpszurlname, string lbszcookiename, string lpszcookiedata );
7
8 Public webbrowsercontrol (string path)
9 {
10 This. url = path;
11 initializecomponent ();
12
13 // set cookie
14 internetsetcookie (URL, "JSESSIONID", globals. thisdocument. sessionid );
15
16 // navigate
17 webbrowser. navigate (URL );
18}
19
20}
2. Send the cookie information of webbrowser to httpwebrequest.
Create a "cookiecontainer" to save the cookie in webbrowser
// Log on to the webbrowser and save the cookie in webbrowser. Document. Cookie. 1 cookiecontainer mycookiecontainer = new cookiecontainer ();
2
3
4 // string cookie to be converted to cookie type and put into cookiecontainer
5 string cookiestr = webbrowser1.document. Cookie;
6 string [] cookstr = cookiestr. Split (';');
7 foreach (string STR in cookstr)
8 {
9 string [] cookienamevalue = Str. Split ('= ');
10 cookie ck = new cookie (cookienamevalue [0]. Trim (). tostring (), cookienamevalue [1]. Trim (). tostring ());
11 CK. Domain = "www.abc.com"; // required
12 mycookiecontainer. Add (CK );
13}
14
15 httpwebrequest hreq = (httpwebrequest) httpwebrequest. Create ("http://www.abc.com/search.asp ");
16 hreq. method = "Post ";
17 hreq. contenttype = "application/X-WWW-form-urlencoded ";
18
19 // cookiecontainer created by yourself
20 hreq. cookiecontainer = mycookiecontainer;
21
22 string postdata = "id = 2005 & Action = SEARCH & name = ";
23 byte [] byte1 = encoding. ASCII. getbytes (postdata );
24 hreq. contentlength = byte1.length;
25
26 stream poststream = hreq. getrequeststream ();
27 poststream. Write (byte1, 0, byte1.length );
28 poststream. Close ();
29
30 httpwebresponse hres = (httpwebresponse) hreq. getresponse ();