C#WebBrowser怎么获取和设置Cookie的方法
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了C#WebBrowser怎么获取和设置Cookie的方法相关的知识,希望对你有一定的参考价值。
参考技术A 如果要设置也是一样的[code=csharp]//设置Cookie
webBrowser1.Document.Cookie=strcookie;[/code]
但是有时候使用这种方法会获取不全,或者是获取不到,下面我提供一个方法
[C#] 纯文本查看 复制代码
?
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
//取当前webBrowser登录后的Cookie值
[DllImport("wininet.dll", CharSet = CharSet.Auto, SetLastError = true)]
static extern bool InternetGetCookieEx(string pchURL, string pchCookieName, StringBuilder pchCookieData, ref int pcchCookieData, int dwFlags, object lpReserved);
//取出Cookie,当登录后才能取
private static string GetCookieString(string url)
// Determine the size of the cookie
int datasize = 256;
StringBuilder cookieData = new StringBuilder(datasize);
if (!InternetGetCookieEx(url, null, cookieData, ref datasize, 0x00002000, null))
if (datasize < 0)
return null;
// Allocate stringbuilder large enough to hold the cookie
cookieData = new StringBuilder(datasize);
if (!InternetGetCookieEx(url, null, cookieData, ref datasize, 0x00002000, null))
return null;
return cookieData.ToString();
在很多情况下我们会使用间进程的webBrowser去实现一些网页的请求和抓去,这个时候有部分网页是取不到Cookie的,那怎么办呢?下面我提供一个方法,应该99%的都能取到
使用方法也很简单
[code=csharp]//获取Cookie
string strcookie=GetCookieString(“你要获取Cookie的URl”);[/code]例如
[code=csharp]//获取Cookie
string strcookie=GetCookieString(“http://www.sufeinet.com”);[/code]
这样获取的是sufeinet.com的Cookie信息 参考技术B 1.在WebBrowser中获取Cookie
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设置cookie!
WebClient wc = new WebClient();
wc.Headers.Add("Cookie", "phpSESSID=" + cookie + ";");
// 注意,这里是Cookie,不是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. 在WebBrowser中设置Cookie
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.将WebBrowser的cookie信息传给HttpWebRequest
先建一个"CookieContainer" 把WebBrowser中的Cookie保存在里面
//在WebBrowser中登录cookie保存在WebBrowser.Document.Cookie中
CookieContainer myCookieContainer = new CookieContainer();
//String 的Cookie 要转成 Cookie型的 并放入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";//必须写对
myCookieContainer.Add(ck);
HttpWebRequest hreq = (HttpWebRequest)HttpWebRequest.Create("http://www.abc.com/search.asp");
hreq.Method = "POST";
hreq.ContentType = "application/x-www-form-urlencoded";
//自己创建的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();本回答被提问者采纳
以上是关于C#WebBrowser怎么获取和设置Cookie的方法的主要内容,如果未能解决你的问题,请参考以下文章