c# HttpWebRequest通過代理服務器抓取網(wǎng)頁內(nèi)容應用介紹
更新時間:2012年11月30日 11:31:48 作者:
在C#項目開發(fā)過程中可能會有些特殊的需求比如:用HttpWebRequest通過代理服務器驗證后抓取網(wǎng)頁內(nèi)容,要想實現(xiàn)此方法并不容易,本文整理了一下,有需求的朋友可以參考下
內(nèi)網(wǎng)用戶或代理上網(wǎng)的用戶使用
using System.IO;
using System.Net;
public string get_html()
{
string urlStr = "http://www.domain.com"; //設定要獲取的地址
HttpWebRequest hwr = (HttpWebRequest)HttpWebRequest.Create(urlStr); //建立HttpWebRequest對象
hwr.Timeout = 60000; //定義服務器超時時間
WebProxy proxy = new WebProxy(); //定義一個網(wǎng)關(guān)對象
proxy.Address = new Uri("http://proxy.domain.com:3128"); //網(wǎng)關(guān)服務器:端口
proxy.Credentials = new NetworkCredential("f3210316", "6978233"); //用戶名,密碼
hwr.UseDefaultCredentials = true; //啟用網(wǎng)關(guān)認証
hwr.Proxy = proxy; //設置網(wǎng)關(guān)
try
{
HttpWebResponse hwrs = (HttpWebResponse)hwr.GetResponse(); //取得回應
}
catch
{
MessageBox.Show("無法連接代理!");
return;
}
//判斷HTTP響應狀態(tài)
if(hwrs.StatusCode != HttpStatusCode.OK)
{
MessageBox.Show("訪問失??!");
hwrs.Close();
return;
}
else
{
Stream s = hwrs.GetResponseStream(); //得到回應的流對象
StreamReader sr = new StreamReader(s, Encoding.UTF8); //以UTF-8編碼讀取流
StringBuilder content = new StringBuilder(); //
while (sr.Peek() != -1) //每次讀取一行,直到
{ //下一個字節(jié)沒有內(nèi)容
content.Append(sr.ReadLine()+""r"n"); //返回為止
} //
//return content.ToString() ;
}
//輸出所有的Header(當然包括服務器輸出的Cookie)
//for(int ii=0;ii<hwrs.Headers.Count;ii++)
//{
//MessageBox.Show(hwrs.Headers.GetKey(ii)+":"+res.Headers[ii]);
//}
}
大家知道,用HttpWebRequest可以通過Http對網(wǎng)頁進行抓取,但是如果是內(nèi)網(wǎng),而且是通過代理上網(wǎng)的用戶,如果直接進行操作是行不通的。
那有沒有什么辦法呢?
當然有,呵呵,見以下代碼:
string urlStr = "http://www.domain.com"; //設定要獲取的地址
HttpWebRequest hwr = (HttpWebRequest)HttpWebRequest.Create(urlStr); //建立HttpWebRequest對象
hwr.Timeout = 60000; //定義服務器超時時間
WebProxy proxy = new WebProxy(); //定義一個網(wǎng)關(guān)對象
proxy.Address = new Uri("http://proxy.domain.com:3128"); //網(wǎng)關(guān)服務器:端口
proxy.Credentials = new NetworkCredential("f3210316", "6978233"); //用戶名,密碼
hwr.UseDefaultCredentials = true; //啟用網(wǎng)關(guān)認証
hwr.Proxy = proxy; //設置網(wǎng)關(guān)
HttpWebResponse hwrs = (HttpWebResponse)hwr.GetResponse(); //取得回應
Stream s = hwrs.GetResponseStream(); //得到回應的流對象
StreamReader sr = new StreamReader(s, Encoding.UTF8); //以UTF-8編碼讀取流
StringBuilder content = new StringBuilder(); //
while (sr.Peek() != -1) //每次讀取一行,直到
{ //下一個字節(jié)沒有內(nèi)容
content.Append(sr.ReadLine()+""r"n"); //返回為止
} //
return content.ToString() ; //返回得到的字符串
復制代碼 代碼如下:
using System.IO;
using System.Net;
public string get_html()
{
string urlStr = "http://www.domain.com"; //設定要獲取的地址
HttpWebRequest hwr = (HttpWebRequest)HttpWebRequest.Create(urlStr); //建立HttpWebRequest對象
hwr.Timeout = 60000; //定義服務器超時時間
WebProxy proxy = new WebProxy(); //定義一個網(wǎng)關(guān)對象
proxy.Address = new Uri("http://proxy.domain.com:3128"); //網(wǎng)關(guān)服務器:端口
proxy.Credentials = new NetworkCredential("f3210316", "6978233"); //用戶名,密碼
hwr.UseDefaultCredentials = true; //啟用網(wǎng)關(guān)認証
hwr.Proxy = proxy; //設置網(wǎng)關(guān)
try
{
HttpWebResponse hwrs = (HttpWebResponse)hwr.GetResponse(); //取得回應
}
catch
{
MessageBox.Show("無法連接代理!");
return;
}
//判斷HTTP響應狀態(tài)
if(hwrs.StatusCode != HttpStatusCode.OK)
{
MessageBox.Show("訪問失??!");
hwrs.Close();
return;
}
else
{
Stream s = hwrs.GetResponseStream(); //得到回應的流對象
StreamReader sr = new StreamReader(s, Encoding.UTF8); //以UTF-8編碼讀取流
StringBuilder content = new StringBuilder(); //
while (sr.Peek() != -1) //每次讀取一行,直到
{ //下一個字節(jié)沒有內(nèi)容
content.Append(sr.ReadLine()+""r"n"); //返回為止
} //
//return content.ToString() ;
}
//輸出所有的Header(當然包括服務器輸出的Cookie)
//for(int ii=0;ii<hwrs.Headers.Count;ii++)
//{
//MessageBox.Show(hwrs.Headers.GetKey(ii)+":"+res.Headers[ii]);
//}
}
大家知道,用HttpWebRequest可以通過Http對網(wǎng)頁進行抓取,但是如果是內(nèi)網(wǎng),而且是通過代理上網(wǎng)的用戶,如果直接進行操作是行不通的。
那有沒有什么辦法呢?
當然有,呵呵,見以下代碼:
復制代碼 代碼如下:
string urlStr = "http://www.domain.com"; //設定要獲取的地址
HttpWebRequest hwr = (HttpWebRequest)HttpWebRequest.Create(urlStr); //建立HttpWebRequest對象
hwr.Timeout = 60000; //定義服務器超時時間
WebProxy proxy = new WebProxy(); //定義一個網(wǎng)關(guān)對象
proxy.Address = new Uri("http://proxy.domain.com:3128"); //網(wǎng)關(guān)服務器:端口
proxy.Credentials = new NetworkCredential("f3210316", "6978233"); //用戶名,密碼
hwr.UseDefaultCredentials = true; //啟用網(wǎng)關(guān)認証
hwr.Proxy = proxy; //設置網(wǎng)關(guān)
HttpWebResponse hwrs = (HttpWebResponse)hwr.GetResponse(); //取得回應
Stream s = hwrs.GetResponseStream(); //得到回應的流對象
StreamReader sr = new StreamReader(s, Encoding.UTF8); //以UTF-8編碼讀取流
StringBuilder content = new StringBuilder(); //
while (sr.Peek() != -1) //每次讀取一行,直到
{ //下一個字節(jié)沒有內(nèi)容
content.Append(sr.ReadLine()+""r"n"); //返回為止
} //
return content.ToString() ; //返回得到的字符串
您可能感興趣的文章:
- C#中的HttpWebRequest類介紹
- C#通過HttpWebRequest發(fā)送帶有JSON Body的POST請求實現(xiàn)
- C#中HttpWebRequest、WebClient、HttpClient的使用詳解
- C#使用HttpWebRequest重定向方法詳解
- C#基于HttpWebRequest實現(xiàn)發(fā)送HTTP請求的方法分析
- C#使用HttpWebRequest與HttpWebResponse模擬用戶登錄
- C# httpwebrequest訪問HTTPS錯誤處理方法
- 淺談C#中HttpWebRequest與HttpWebResponse的使用方法
- C#中HttpWebRequest的用法詳解
- C#采用HttpWebRequest實現(xiàn)保持會話上傳文件到HTTP的方法
- C#中的HttpWebRequest類用法詳解
相關(guān)文章
C#使用opencv截取旋轉(zhuǎn)矩形區(qū)域圖像的實現(xiàn)示例
這篇文章主要介紹了C#使用opencv截取旋轉(zhuǎn)矩形區(qū)域圖像,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2021-03-03C#中通過使用Connection類來實現(xiàn)打開/關(guān)閉數(shù)據(jù)庫的代碼實例
今天小編就為大家分享一篇關(guān)于C#中通過使用Connection類來實現(xiàn)打開/關(guān)閉數(shù)據(jù)庫的代碼實例,小編覺得內(nèi)容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧2018-10-10Unity之Luaframework框架lua調(diào)用C#方法
這篇文章主要介紹了Unity之Luaframework框架lua調(diào)用C#方法,在這里需要寫一個C#腳本,腳本里寫方法需要在lua中調(diào)用,具體實例代碼參考下本文吧2021-09-09