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)關對象
proxy.Address = new Uri("http://proxy.domain.com:3128"); //網(wǎng)關服務器:端口
proxy.Credentials = new NetworkCredential("f3210316", "6978233"); //用戶名,密碼
hwr.UseDefaultCredentials = true; //啟用網(wǎng)關認証
hwr.Proxy = proxy; //設置網(wǎng)關
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)關對象
proxy.Address = new Uri("http://proxy.domain.com:3128"); //網(wǎng)關服務器:端口
proxy.Credentials = new NetworkCredential("f3210316", "6978233"); //用戶名,密碼
hwr.UseDefaultCredentials = true; //啟用網(wǎng)關認証
hwr.Proxy = proxy; //設置網(wǎng)關
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)關對象
proxy.Address = new Uri("http://proxy.domain.com:3128"); //網(wǎng)關服務器:端口
proxy.Credentials = new NetworkCredential("f3210316", "6978233"); //用戶名,密碼
hwr.UseDefaultCredentials = true; //啟用網(wǎng)關認証
hwr.Proxy = proxy; //設置網(wǎng)關
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)關對象
proxy.Address = new Uri("http://proxy.domain.com:3128"); //網(wǎng)關服務器:端口
proxy.Credentials = new NetworkCredential("f3210316", "6978233"); //用戶名,密碼
hwr.UseDefaultCredentials = true; //啟用網(wǎng)關認証
hwr.Proxy = proxy; //設置網(wǎng)關
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類用法詳解
相關文章
C#實現(xiàn)完善Excel不規(guī)則合并單元格數(shù)據(jù)導入的示例代碼
本文主要介紹了C#實現(xiàn)完善Excel不規(guī)則合并單元格數(shù)據(jù)導入的示例代碼,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2025-02-02
c#異步操作后臺運行(backgroundworker類)示例
這篇文章主要介紹了c#異步操作后臺運行(backgroundworker類)示例,需要的朋友可以參考下2014-04-04

