好湿?好紧?好多水好爽自慰,久久久噜久噜久久综合,成人做爰A片免费看黄冈,机机对机机30分钟无遮挡

主頁 > 知識庫 > asp.net c#采集需要登錄頁面的實現原理及代碼

asp.net c#采集需要登錄頁面的實現原理及代碼

熱門標簽:蘋果手機凱立德地圖標注 百度ai地圖標注 玉林市機器人外呼系統哪家好 電話機器人軟件銷售工作 預測式外呼系統使用說明 南陽外呼系統定制化 同安公安400電話怎么申請流程 申請400電話手續 合肥電銷外呼系統哪家公司做的好
首先說明:代碼片段是從網絡獲取,然后自己修改。我想好的東西應該拿來分享。

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

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

在這里我提供2種方法。
第一種,直接將Cookie信息放入HttpWebRequest的CookieContainer里。看代碼:
復制代碼 代碼如下:

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
{
//獲取提交的字節
byte[] bs = Encoding.UTF8.GetBytes(content);
//設置提交的相關參數
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;
//提交請求數據
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);
}
}

第二種,每次打開采集程序時,需要先到被采集的網站模擬登錄一次,獲取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");
//將提交的字符串數據轉換成字節數組
byte[] postData = Encoding.UTF8.GetBytes(postString);
//設置提交的相關參數
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;
// 提交請求數據
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。

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

標簽:南京 海南 臺州 淄博 嘉興 南昌 南京 揚州

巨人網絡通訊聲明:本文標題《asp.net c#采集需要登錄頁面的實現原理及代碼》,本文關鍵詞  asp.net,采集,需要,登錄,頁,;如發現本文內容存在版權問題,煩請提供相關信息告之我們,我們將及時溝通與處理。本站內容系統采集于網絡,涉及言論、版權與本站無關。
  • 相關文章
  • 下面列出與本文章《asp.net c#采集需要登錄頁面的實現原理及代碼》相關的同類信息!
  • 本頁收集關于asp.net c#采集需要登錄頁面的實現原理及代碼的相關信息資訊供網民參考!
  • 推薦文章