從Internet上抓取指定URL的源碼的方案
http://www.cnblogs.com/huqingyu/archive/2004/07/26/27355.aspx 引言:
在做無線專案的時候,與通訊公司的資料通訊有一部分是通過XML交互的,所以必須要動態抓取通訊公司提供的固定的Internet上的資料,便研究了一下如何抓取固定url上的資料,現與大家分享一下。
類名GetPageCode,有一個方法GetSource,通過屬性傳遞參數,入參控制的是要取得URL的位址,代理伺服器的設置及輸出方式的控制,這裏大家可以再擴展自己的需要,我這裏只提供了兩種方式,一種是直接寫到本地的某個檔中,另外一種就是返回字串的。類裏已經作了比較詳細的注釋,我想大家很容易就看明白了,如果實在不明白,那就msn上問吧,MSN:yubo@x263.net。 調用方式:
#region 測試獲取遠程網頁
GetPageCode gpc = new GetPageCode();
gpc.Url="http://ppcode.com";
gpc.ProxyState=1;//使用代理伺服器,0為不使用,設置為1後下面的代理設置才起作用
gpc.ProxyAddress="http://proxyName.com";//代理伺服器位址
gpc.ProxyPort="80";//代理伺服器的埠
gpc.ProxyAccount="proxy";//代理伺服器帳號
gpc.ProxyPassword="password";//代理伺服器密碼
gpc.ProxyDomain="bqc";//代理伺服器域
gpc.OutFilePath=filePath;//設置輸出檔路徑的地方,如果不設置,則返回字串
gpc.GetSource();//處理
string tempErr=gpc.NoteMessage;//如果出錯,這裏會提示
string tempCode=gpc.OutString;//返回的字串
#endregion
類代碼:
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Net;
using System.Text;
using System.Web;
namespace Test.Com
{
///
/// 功能:取得Internet上的URL頁的源碼
/// 創建:2004-03-22
/// 作者:Rexsp MSN:yubo@x263.net
///
public class GetPageCode
{
#region 私有變數
///
/// 網頁URL地址
///
private string url=null;
///
/// 是否使用代碼伺服器:0 不使用 1 使用代理伺服器
///
private int proxyState=0;
///
/// 代理伺服器位址
///
private string proxyAddress=null;
///
/// 代理伺服器埠
///
private string proxyPort=null;
///
/// 代理伺服器用戶名
///
private string proxyAccount=null;
///
/// 代理伺服器密碼
///
private string proxyPassword=null;
///
/// 代理伺服器域
///
private string proxyDomain=null;
///
/// 輸出檔路徑
///
private string outFilePath=null;
///
/// 輸出的字串
///
private string outString=null;
///
/// 提示資訊
///
private string noteMessage; #endregion #region 公共屬性
///
/// 欲讀取的URL地址
///
public string Url
{
get{return url;}
set{url=value;}
}
///
/// 是否使用代理伺服器標誌
///
public int ProxyState
{
get{return proxyState;}
set{proxyState=value;}
}
///
/// 代理伺服器位址
///
public string ProxyAddress
{
get{return proxyAddress;}
set{proxyAddress=value;}
}
///
/// 代理伺服器埠
///
public string ProxyPort
{
get{return proxyPort;}
set{proxyPort=value;}
}
///
/// 代理伺服器帳號
///
public string ProxyAccount
{
get{return proxyAccount;}
set{proxyAccount=value;}
}
///
/// 代理伺服器密碼
///
public string ProxyPassword
{
get{return proxyPassword;}
set{proxyPassword=value;}
}
///
/// 代理伺服器域
///
public string ProxyDomain
{
get{return proxyDomain;}
set{proxyDomain=value;}
}
///
/// 輸出檔路徑
///
public string OutFilePath
{
get{return outFilePath;}
set{outFilePath=value;}
}
///
/// 返回的字串
///
public string OutString
{
get{return outString;}
}
///
/// 返回提示資訊
///
public string NoteMessage
{
get{return noteMessage;}
}
#endregion
#region 構造函數
public GetPageCode()
{
}
#endregion #region 公共方法
///
/// 讀取指定URL地址,存到指定檔中
///
public void GetSource()
{
WebRequest request = WebRequest.Create(this.url);
//使用代理伺服器的處理
if(this.proxyState==1)
{
//默認讀取80埠的數據
if(this.proxyPort==null)
this.ProxyPort="80"; WebProxy myProxy=new WebProxy();
myProxy = (WebProxy)request.Proxy;
myProxy.Address = new Uri(this.ProxyAddress ":" this.ProxyPort);
myProxy.Credentials = new NetworkCredential(this.proxyAccount, this.proxyPassword, this.ProxyDomain);
request.Proxy = myProxy;
}
try
{
//請求服務
WebResponse response = request.GetResponse();
//返回資訊
Stream resStream = response.GetResponseStream();
StreamReader sr = new StreamReader(resStream, System.Text.Encoding.Default);
string tempCode= sr.ReadToEnd();
resStream.Close();
sr.Close(); //如果輸出檔路徑為空,便將得到的內容賦給OutString屬性
if(this.outFilePath==null)
{
this.outString=tempCode;
}
else
{ FileInfo fi = new FileInfo(this.outFilePath);
//如果存在檔則先幹掉
if(fi.Exists)
fi.Delete();
StreamWriter sw = new StreamWriter(this.outFilePath,true,Encoding.Default);
sw.Write(tempCode);
sw.Flush();
sw.Close();
}
}
catch
{
this.noteMessage="出錯了,請檢查網路是否連通;";
} }
#endregion }
}