從Internet上抓取指定URL的源碼的方案(C#)

來源:互聯網
上載者:User


調用方式:

#region 測試擷取遠程網頁

GetPageCode gpc = new GetPageCode();

gpc.Url="http://ppcode.com/";

gpc.ProxyState=1;//使用Proxy 伺服器,0為不使用,設定為1後下面的代理設定才起作用

gpc.ProxyAddress="http://proxyName.com";//Proxy 伺服器地址

gpc.ProxyPort="80";//Proxy 伺服器的連接埠

gpc.ProxyAccount="proxy";//Proxy 伺服器帳號

gpc.ProxyPassword="password";//Proxy 伺服器密碼

gpc.ProxyDomain="bqc";//Proxy 伺服器域

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

{

/// <summary>

/// 功能:取得Internet上的URL頁的源碼

/// 建立:2004-03-22

/// 作者:Rexsp MSN:yubo@x263.net

/// </summary>

public class GetPageCode

{

#region 私人變數

/// <summary>

/// 網頁URL地址

/// </summary>

private string url=null;

/// <summary>

/// 是否使用代碼伺服器:0 不使用 1 使用Proxy 伺服器

/// </summary>

private int proxyState=0;

/// <summary>

/// Proxy 伺服器地址

/// </summary>

private string proxyAddress=null;

/// <summary>

/// Proxy 伺服器連接埠

/// </summary>

private string proxyPort=null;

/// <summary>

/// Proxy 伺服器使用者名稱

/// </summary>

private string proxyAccount=null;

/// <summary>

/// Proxy 伺服器密碼

/// </summary>

private string proxyPassword=null;

/// <summary>

/// Proxy 伺服器域

/// </summary>

private string proxyDomain=null;

/// <summary>

/// 輸出檔案路徑

/// </summary>

private string outFilePath=null;

/// <summary>

/// 輸出的字串

/// </summary>

private string outString=null;

/// <summary>

/// 提示資訊

/// </summary>

private string noteMessage;



#endregion



#region 公用屬性

/// <summary>

/// 欲讀取的URL地址

/// </summary>

public string Url

{

get{return url;}

set{url=value;}

}

/// <summary>

/// 是否使用Proxy 伺服器標誌

/// </summary>

public int ProxyState

{

get{return proxyState;}

set{proxyState=value;}

}

/// <summary>

/// Proxy 伺服器地址

/// </summary>

public string ProxyAddress

{

get{return proxyAddress;}

set{proxyAddress=value;}

}

/// <summary>

/// Proxy 伺服器連接埠

/// </summary>

public string ProxyPort

{

get{return proxyPort;}

set{proxyPort=value;}

}

/// <summary>

/// Proxy 伺服器帳號

/// </summary>

public string ProxyAccount

{

get{return proxyAccount;}

set{proxyAccount=value;}

}

/// <summary>

/// Proxy 伺服器密碼

/// </summary>

public string ProxyPassword

{

get{return proxyPassword;}

set{proxyPassword=value;}

}

/// <summary>

/// Proxy 伺服器域

/// </summary>

public string ProxyDomain

{

get{return proxyDomain;}

set{proxyDomain=value;}

}

/// <summary>

/// 輸出檔案路徑

/// </summary>

public string OutFilePath

{

get{return outFilePath;}

set{outFilePath=value;}

}

/// <summary>

/// 返回的字串

/// </summary>

public string OutString

{

get{return outString;}



}

/// <summary>

/// 返回提示資訊

/// </summary>

public string NoteMessage

{

get{return noteMessage;}



}



#endregion



#region 建構函式

public GetPageCode()

{

}

#endregion



#region 公用方法

/// <summary>

/// 讀取指定URL地址,存到指定檔案中

/// </summary>

public void GetSource()

{

WebRequest request = WebRequest.Create(this.url);

//使用Proxy 伺服器的處理

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



}

}







聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.