asp.net c#采集需要登錄頁面的實現(xiàn)原理及代碼
更新時間:2013年02月01日 16:39:02 作者:
當我們采集頁面的時候,如果被采集的網(wǎng)站需要登錄才能采集,原理搞清楚了,就好辦了,我們所要做的僅僅是在采集的時候(或者說HttpWebRequest提交數(shù)據(jù)的時候),將Cookie信息放入Http請求頭里面就可以了,感興趣的朋友可以了解下,或許對你有所幫助
首先說明:代碼片段是從網(wǎng)絡獲取,然后自己修改。我想好的東西應該拿來分享。
實現(xiàn)原理:當我們采集頁面的時候,如果被采集的網(wǎng)站需要登錄才能采集。不管是基于Cookie還是基于Session,我們都會首先發(fā)送一個Http請求頭,這個Http請求頭里面就包含了網(wǎng)站需要的Cookie信息。當網(wǎng)站接收到發(fā)送過來的Http請求頭時,會從Http請求頭獲取相關的Cookie或者Session信息,然后由程序來處理,決定你是否有權限訪問當前頁面。
好了,原理搞清楚了,就好辦了。我們所要做的僅僅是在采集的時候(或者說HttpWebRequest提交數(shù)據(jù)的時候),將Cookie信息放入Http請求頭里面就可以了。
在這里我提供2種方法。
第一種,直接將Cookie信息放入HttpWebRequest的CookieContainer里??创a:
protected void Page_Load(object sender, EventArgs e)
{
//設置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ù)
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);
}
}
第二種,每次打開采集程序時,需要先到被采集的網(wǎng)站模擬登錄一次,獲取CookieContainer,然后再采集??创a:
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");
//將提交的字符串數(shù)據(jù)轉(zhuǎn)換成字節(jié)數(shù)組
byte[] postData = Encoding.UTF8.GetBytes(postString);
//設置提交的相關參數(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);
}
}
也許有人會問,如果對方登錄的時候要驗證碼怎么辦?那你就用第一種方式吧,只不過需要你分析對方的Cookie。
應用范圍:采集數(shù)據(jù)、論壇發(fā)帖、博客發(fā)文。
實現(xiàn)原理:當我們采集頁面的時候,如果被采集的網(wǎng)站需要登錄才能采集。不管是基于Cookie還是基于Session,我們都會首先發(fā)送一個Http請求頭,這個Http請求頭里面就包含了網(wǎng)站需要的Cookie信息。當網(wǎng)站接收到發(fā)送過來的Http請求頭時,會從Http請求頭獲取相關的Cookie或者Session信息,然后由程序來處理,決定你是否有權限訪問當前頁面。
好了,原理搞清楚了,就好辦了。我們所要做的僅僅是在采集的時候(或者說HttpWebRequest提交數(shù)據(jù)的時候),將Cookie信息放入Http請求頭里面就可以了。
在這里我提供2種方法。
第一種,直接將Cookie信息放入HttpWebRequest的CookieContainer里??创a:
復制代碼 代碼如下:
protected void Page_Load(object sender, EventArgs e)
{
//設置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ù)
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);
}
}
第二種,每次打開采集程序時,需要先到被采集的網(wǎng)站模擬登錄一次,獲取CookieContainer,然后再采集??创a:
復制代碼 代碼如下:
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");
//將提交的字符串數(shù)據(jù)轉(zhuǎn)換成字節(jié)數(shù)組
byte[] postData = Encoding.UTF8.GetBytes(postString);
//設置提交的相關參數(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);
}
}
也許有人會問,如果對方登錄的時候要驗證碼怎么辦?那你就用第一種方式吧,只不過需要你分析對方的Cookie。
應用范圍:采集數(shù)據(jù)、論壇發(fā)帖、博客發(fā)文。
相關文章
ASP.NET仿新浪微博下拉加載更多數(shù)據(jù)瀑布流效果
本篇文章介紹了如何實現(xiàn)下拉加載更多數(shù)據(jù)瀑布流的效果,這種效果最近很流行,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2015-07-07ASP.NET單選按鈕控件RadioButton常用屬性和方法介紹
RadioButton又稱單選按鈕,其在工具箱中的圖標為 ,單選按鈕通常成組出現(xiàn),用于提供兩個或多個互斥選項,即在一組單選鈕中只能選擇一個2014-04-04Entity?Framework生成DataBase?First模式
本文詳細講解了Entity?Framework生成DataBase?First模式的方法,文中通過示例代碼介紹的非常詳細。對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2022-03-03asp.net 關于==?:和if()else()條件判斷等效例子
關于==?:和if()else() 等效例子2010-03-03使用.net core3.0 正式版創(chuàng)建Winform程序的方法(圖文)
這篇文章主要介紹了使用.net core3.0 正式版創(chuàng)建Winform程序的方法,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2020-03-03