1
public partial class WebBrowserControl : Form
2
{
3
private String url;
4![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
5
[DllImport("wininet.dll", CharSet = CharSet.Auto, SetLastError = true)]
6
public static extern bool InternetSetCookie(string lpszUrlName, string lbszCookieName, string lpszCookieData);
7![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
8
public WebBrowserControl(String path)
9
{
10
this.url = path;
11
InitializeComponent();
12![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
13
// set cookie
14
InternetSetCookie(url, "JSESSIONID", Globals.ThisDocument.sessionID);
15![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
16
// navigate
17
webBrowser.Navigate(url);
18
}
19
![](https://www.cnblogs.com/Images/dot.gif)
20
}
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
2
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
3
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
4
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
5
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
6
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
7
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
8
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
9
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
10
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
11
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
12
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
13
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
14
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
15
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
16
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
17
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
18
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
19
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/dot.gif)
20
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)
2、将WebBrowser的cookie信息传给HttpWebRequest.
先建一个"CookieContainer" 把WebBrowser中的Cookie保存在里面
//在WebBrowser中登录 cookie保存在 WebBrowser.Document.Cookie中
1
CookieContainer myCookieContainer = new CookieContainer();
2![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
3![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
4
//String 的Cookie 要转成 Cookie型的 并放入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";//必须写对
12
myCookieContainer.Add(ck);
13
}
14![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
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
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();
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
2
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
3
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
4
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
5
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
6
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
7
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
8
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
9
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
10
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
11
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
12
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
13
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)
14
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
15
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
16
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
17
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
18
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
19
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
20
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
21
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
22
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
23
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
24
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
25
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
26
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
27
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
28
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
29
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
30
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)