鉴客 C# 抓取页面(带认证)

1. [代码][C#]代码    

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

2. [代码]Cookie 处理     跳至 [1] [2] [全屏预览]

01CookieCollection cookiesResponse = new CookieCollection();
02 
03if (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}

转载于:https://www.cnblogs.com/xuxiaoshuan/p/3628945.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值