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