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對(duì)象
hwr.Timeout = 60000; //定義服務(wù)器超時(shí)時(shí)間
WebProxy proxy = new WebProxy(); //定義一個(gè)網(wǎng)關(guān)對(duì)象
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)的流對(duì)象
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對(duì)網(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對(duì)象
hwr.Timeout = 60000; //定義服務(wù)器超時(shí)時(shí)間
WebProxy proxy = new WebProxy(); //定義一個(gè)網(wǎng)關(guān)對(duì)象
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)的流對(duì)象
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對(duì)象
hwr.Timeout = 60000; //定義服務(wù)器超時(shí)時(shí)間
WebProxy proxy = new WebProxy(); //定義一個(gè)網(wǎng)關(guān)對(duì)象
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)的流對(duì)象
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對(duì)網(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對(duì)象
hwr.Timeout = 60000; //定義服務(wù)器超時(shí)時(shí)間
WebProxy proxy = new WebProxy(); //定義一個(gè)網(wǎng)關(guān)對(duì)象
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)的流對(duì)象
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請(qǐng)求實(shí)現(xiàn)
- C#中HttpWebRequest、WebClient、HttpClient的使用詳解
- C#使用HttpWebRequest重定向方法詳解
- C#基于HttpWebRequest實(shí)現(xiàn)發(fā)送HTTP請(qǐng)求的方法分析
- C#使用HttpWebRequest與HttpWebResponse模擬用戶登錄
- C# httpwebrequest訪問HTTPS錯(cuò)誤處理方法
- 淺談C#中HttpWebRequest與HttpWebResponse的使用方法
- C#中HttpWebRequest的用法詳解
- C#采用HttpWebRequest實(shí)現(xiàn)保持會(huì)話上傳文件到HTTP的方法
- C#中的HttpWebRequest類用法詳解
相關(guān)文章
C#實(shí)現(xiàn)完善Excel不規(guī)則合并單元格數(shù)據(jù)導(dǎo)入的示例代碼
本文主要介紹了C#實(shí)現(xiàn)完善Excel不規(guī)則合并單元格數(shù)據(jù)導(dǎo)入的示例代碼,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2025-02-02
c#異步操作后臺(tái)運(yùn)行(backgroundworker類)示例
這篇文章主要介紹了c#異步操作后臺(tái)運(yùn)行(backgroundworker類)示例,需要的朋友可以參考下2014-04-04

