佳木斯湛栽影视文化发展公司

主頁 > 知識庫 > asp.net c#采集需要登錄頁面的實(shí)現(xiàn)原理及代碼

asp.net c#采集需要登錄頁面的實(shí)現(xiàn)原理及代碼

熱門標(biāo)簽:電子圍欄 Mysql連接數(shù)設(shè)置 科大訊飛語音識別系統(tǒng) 團(tuán)購網(wǎng)站 Linux服務(wù)器 阿里云 服務(wù)器配置 銀行業(yè)務(wù)
首先說明:代碼片段是從網(wǎng)絡(luò)獲取,然后自己修改。我想好的東西應(yīng)該拿來分享。

實(shí)現(xiàn)原理:當(dāng)我們采集頁面的時(shí)候,如果被采集的網(wǎng)站需要登錄才能采集。不管是基于Cookie還是基于Session,我們都會首先發(fā)送一個(gè)Http請求頭,這個(gè)Http請求頭里面就包含了網(wǎng)站需要的Cookie信息。當(dāng)網(wǎng)站接收到發(fā)送過來的Http請求頭時(shí),會從Http請求頭獲取相關(guān)的Cookie或者Session信息,然后由程序來處理,決定你是否有權(quán)限訪問當(dāng)前頁面。

好了,原理搞清楚了,就好辦了。我們所要做的僅僅是在采集的時(shí)候(或者說HttpWebRequest提交數(shù)據(jù)的時(shí)候),將Cookie信息放入Http請求頭里面就可以了。

在這里我提供2種方法。
第一種,直接將Cookie信息放入HttpWebRequest的CookieContainer里??创a:
復(fù)制代碼 代碼如下:

protected void Page_Load(object sender, EventArgs e)
{
//設(shè)置Cookie,存入Hashtable
Hashtable ht = new Hashtable();
ht.Add("username", "youraccount");
ht.Add("id", "yourid");
this.Collect(ht);
}
public void Collect(Hashtable ht)
{
string content = string.Empty;
string url = "http://www.ibest100.com/需要登錄后才能采集的頁面";
string host = "http://www.ibest100.com";
try
{
//獲取提交的字節(jié)
byte[] bs = Encoding.UTF8.GetBytes(content);
//設(shè)置提交的相關(guān)參數(shù)
HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create(url);
req.Method = "POST";
req.ContentType = "application/json;charset=utf-8";
req.ContentLength = bs.Length;
//將Cookie放入CookieContainer,然后再將CookieContainer添加到HttpWebRequest
CookieContainer cc = new CookieContainer();
cc.Add(new Uri(host), new Cookie("username", ht["username"].ToString()));
cc.Add(new Uri(host), new Cookie("id", ht["id"].ToString()));
req.CookieContainer = cc;
//提交請求數(shù)據(jù)
Stream reqStream = req.GetRequestStream();
reqStream.Write(bs, 0, bs.Length);
reqStream.Close();
//接收返回的頁面,必須的,不能省略
WebResponse wr = req.GetResponse();
System.IO.Stream respStream = wr.GetResponseStream();
System.IO.StreamReader reader = new System.IO.StreamReader(respStream, System.Text.Encoding.GetEncoding("utf-8"));
string t = reader.ReadToEnd();
System.Web.HttpContext.Current.Response.Write(t);
wr.Close();
}
catch (Exception ex)
{
System.Web.HttpContext.Current.Response.Write("異常在getPostRespone:" + ex.Source + ":" + ex.Message);
}
}

第二種,每次打開采集程序時(shí),需要先到被采集的網(wǎng)站模擬登錄一次,獲取CookieContainer,然后再采集??创a:
復(fù)制代碼 代碼如下:

protected void Page_Load(object sender, EventArgs e)
{
try
{
CookieContainer cookieContainer = new CookieContainer();
string formatString = "username={0}password={1}";//***************
string postString = string.Format(formatString, "youradminaccount", "yourpassword");
//將提交的字符串?dāng)?shù)據(jù)轉(zhuǎn)換成字節(jié)數(shù)組
byte[] postData = Encoding.UTF8.GetBytes(postString);
//設(shè)置提交的相關(guān)參數(shù)
string URI = "http://www.ibest100.com/登錄頁面";//***************
HttpWebRequest request = WebRequest.Create(URI) as HttpWebRequest;
request.Method = "POST";
request.KeepAlive = false;
request.ContentType = "application/x-www-form-urlencoded";
request.CookieContainer = cookieContainer;
request.ContentLength = postData.Length;
// 提交請求數(shù)據(jù)
System.IO.Stream outputStream = request.GetRequestStream();
outputStream.Write(postData, 0, postData.Length);
outputStream.Close();
//接收返回的頁面,必須的,不能省略
HttpWebResponse response = request.GetResponse() as HttpWebResponse;
System.IO.Stream responseStream = response.GetResponseStream();
System.IO.StreamReader reader = new System.IO.StreamReader(responseStream, Encoding.GetEncoding("gb2312"));
string srcString = reader.ReadToEnd();
//打開您要訪問的頁面
URI = "http://www.ibest100.com/需要登錄后才能采集的頁面";//***************
request = WebRequest.Create(URI) as HttpWebRequest;
request.Method = "GET";
request.KeepAlive = false;
request.CookieContainer = cookieContainer;
// 接收返回的頁面
response = request.GetResponse() as HttpWebResponse;
responseStream = response.GetResponseStream();
reader = new System.IO.StreamReader(responseStream, Encoding.GetEncoding("gb2312"));
srcString = reader.ReadToEnd();
//輸出獲取的頁面或者處理
Response.Write(srcString);
}
catch (WebException we)
{
string msg = we.Message;
Response.Write(msg);
}
}

也許有人會問,如果對方登錄的時(shí)候要驗(yàn)證碼怎么辦?那你就用第一種方式吧,只不過需要你分析對方的Cookie。

應(yīng)用范圍:采集數(shù)據(jù)、論壇發(fā)帖、博客發(fā)文。
您可能感興趣的文章:
  • C# Winform中實(shí)現(xiàn)主窗口打開登錄窗口關(guān)閉的方法
  • C#實(shí)現(xiàn)簡單的登錄界面
  • div彈出層的ajax登錄(Jquery版+c#)
  • C#.NET實(shí)現(xiàn)網(wǎng)頁自動登錄的方法
  • C#實(shí)現(xiàn)的三種模擬自動登錄和提交POST信息的方法
  • C#實(shí)現(xiàn)登錄窗口(不用隱藏)
  • .NET C#使用微信公眾號登錄網(wǎng)站
  • C#中登錄窗體和歡迎窗體關(guān)閉方法分析
  • c#通過進(jìn)程調(diào)用cmd判斷登錄用戶權(quán)限代碼分享
  • c#通用登錄模塊分享
  • C#實(shí)現(xiàn)的WINDOWS登錄功能示例

標(biāo)簽:衢州 衡水 江蘇 棗莊 萍鄉(xiāng) 蚌埠 廣元 大理

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《asp.net c#采集需要登錄頁面的實(shí)現(xiàn)原理及代碼》,本文關(guān)鍵詞  ;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無關(guān)。
  • 相關(guān)文章
  • 收縮
    • 微信客服
    • 微信二維碼
    • 電話咨詢

    • 400-1100-266
    醴陵市| 东兰县| 黄山市| 隆尧县| 来安县| 平度市| 陆川县| 将乐县| 额尔古纳市| 松潘县| 靖边县| 民权县| 辽中县| 榆林市| 渑池县| 鄂托克前旗| 东城区| 临沧市| 霍邱县| 潞西市| 酉阳| 门源| 固镇县| 东平县| 五大连池市| 洪江市| 龙陵县| 炎陵县| 桃源县| 长春市| 大田县| 本溪市| 宜川县| 高要市| 沽源县| 隆化县| 万载县| 房山区| 抚顺市| 池州市| 怀宁县|