让代码更简单

C#从WebBrowser中取得Cookie 并在 HttpWebRequest中使用

重要:本文最后更新于2018-11-13 13:52:45,某些文章具有时效性,若有错误或已失效,请在下方留言或联系代码狗

C#核心代码如下:

复制
webBrowser1.Navigate("http://www.cqooc.com/learn/mooc/forum?id=334564183#nogo");//浏览器控件加载页面

CookieContainer myCookieContainer = new CookieContainer();

//String 的Cookie 要转成 Cookie型的 并放入CookieContainer中
 string cookieStr = webBrowser1.Document.Cookie;
 string[] cookstr = cookieStr.Split(';');
//遍历取出cookies并构造新的cookies信息
foreach (string str in cookstr)
 {
 string[] cookieNameValue = str.Split('=');
 Cookie ck = new Cookie(cookieNameValue[0].Trim().ToString(), cookieNameValue[1].Trim().ToString());
 ck.Domain = "www.cqooc.com";//cookies信息域名
 myCookieContainer.Add(ck);
 }
 HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://www.cqooc.com/json/forums");
 request.CookieContainer = new CookieContainer();
 CookieContainer cookie = request.CookieContainer;//如果用不到Cookie,删去即可
 //以下是发送的http头,随便加,其中referer挺重要的,有些网站会根据这个来反盗链
 request.Referer = "http://www.cqooc.com/json/forums";
 string cookStr = webBrowser1.Document.Cookie;
 request.CookieContainer=myCookieContainer;//设置访问cookies信息
 request.Accept = "Accept:text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8";
 request.Headers["Accept-Language"] = "zh-CN,zh;q=0.";
 request.Headers["Accept-Charset"] = "GBK,utf-8;q=0.7,*;q=0.3";
 request.UserAgent = "User-Agent:Mozilla/5.0 (Windows NT 5.1) AppleWebKit/535.1 (KHTML, like Gecko) Chrome/14.0.835.202 Safari/535.1";
 request.KeepAlive = true;
 //上面的http头看情况而定,但是下面俩必须加
 request.ContentType = "application/x-www-form-urlencoded";
 request.Method = "POST";

Encoding encoding = Encoding.UTF8;//根据网站的编码自定义
 string postDataStr = “post数据”;
 byte[] postData = encoding.GetBytes(postDataStr);
 request.ContentLength = postData.Length;
 Stream requestStream = request.GetRequestStream();
 requestStream.Write(postData, 0, postData.Length);

HttpWebResponse response = (HttpWebResponse)request.GetResponse();
 Stream responseStream = response.GetResponseStream();
 //如果http头中接受gzip的话,这里就要判断是否为有压缩,有的话,直接解压缩即可
 if (response.Headers["Content-Encoding"] != null && response.Headers["Content-Encoding"].ToLower().Contains("gzip"))
 {
 responseStream = new GZipStream(responseStream, CompressionMode.Decompress);
 }

StreamReader streamReader = new StreamReader(responseStream, encoding);
 string retString = streamReader.ReadToEnd();
 textBox1.Text = retString;
 streamReader.Close();
 responseStream.Close();

感觉很棒!可以赞赏支持我哟~

2 打赏

评论 (0)

登录后评论
QQ咨询 邮件咨询 狗哥推荐