• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

C# 抓取页面(带认证) 转

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

[C#]代码

01 HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create("");
02 req.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; .NET CLR 1.0.3705;)";
03 req.Method = "POST";
04 req.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8";
05 req.Headers.Add("Accept-Language: en-us,en;q=0.5");
06 req.Headers.Add("Accept-Encoding: gzip,deflate");
07 req.Headers.Add("Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7");
08 req.KeepAlive = true;
09 req.Headers.Add("Keep-Alive: 300");
10 req.Referer = "copy from url";
11  
12 req.ContentType = "application/x-www-form-urlencoded";
13  
14 String Username = copy from url;
15 String PassWord = copy from url;
16  
17 StreamWriter sw = new StreamWriter(req.GetRequestStream());
18 sw.Write(string.Format("&loginname={0}&password={1}&btnSubmit=Log In&institutioncode=H4V9KLUT45AV&version=2", Username, PassWord));
19 sw.Close();
20 HttpWebResponse response = (HttpWebResponse)req.GetResponse();
21  
22 StreamReader reader = new StreamReader(response.GetResponseStream());
23 string tmp = reader.ReadToEnd();

[代码] Cookie 处理

01 CookieCollection cookiesResponse = new CookieCollection();
02  
03 if (response != null)
04 {
05     foreach (string cookie in response.Headers["Set-Cookie"].Split(';'))
06     {
07         string name = cookie.Split('=')[0];
08         string value = cookie.Substring(name.Length + 1);
09         cookiesResponse.Add(new Cookie(name.Trim(), value.Trim(), path, domain));
10     }
11 }

鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
cf997C. Sky Full of Stars(组合数 容斥)发布时间:2022-07-13
下一篇:
C语言宏展开发布时间:2022-07-13
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap