麻豆小视频在线观看_中文黄色一级片_久久久成人精品_成片免费观看视频大全_午夜精品久久久久久久99热浪潮_成人一区二区三区四区

首頁 > 編程 > .NET > 正文

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

2024-07-21 02:18:22
字體:
來源:轉載
供稿:網友


調用方式:

#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

{

/// <summary>

/// 功能:取得internet上的url頁的源碼

/// 創建:2004-03-22

/// 作者:rexsp msn:[email protected]

/// </summary>

public class getpagecode

{

#region 私有變量

/// <summary>

/// 網頁url地址

/// </summary>

private string url=null;

/// <summary>

/// 是否使用代碼服務器:0 不使用 1 使用代理服務器

/// </summary>

private int proxystate=0;

/// <summary>

/// 代理服務器地址

/// </summary>

private string proxyaddress=null;

/// <summary>

/// 代理服務器端口

/// </summary>

private string proxyport=null;

/// <summary>

/// 代理服務器用戶名

/// </summary>

private string proxyaccount=null;

/// <summary>

/// 代理服務器密碼

/// </summary>

private string proxypassword=null;

/// <summary>

/// 代理服務器域

/// </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>

/// 是否使用代理服務器標志

/// </summary>

public int proxystate

{

get{return proxystate;}

set{proxystate=value;}

}

/// <summary>

/// 代理服務器地址

/// </summary>

public string proxyaddress

{

get{return proxyaddress;}

set{proxyaddress=value;}

}

/// <summary>

/// 代理服務器端口

/// </summary>

public string proxyport

{

get{return proxyport;}

set{proxyport=value;}

}

/// <summary>

/// 代理服務器賬號

/// </summary>

public string proxyaccount

{

get{return proxyaccount;}

set{proxyaccount=value;}

}

/// <summary>

/// 代理服務器密碼

/// </summary>

public string proxypassword

{

get{return proxypassword;}

set{proxypassword=value;}

}

/// <summary>

/// 代理服務器域

/// </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);

//使用代理服務器的處理

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



}

}






發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 国产精品久久久免费 | 本站只有精品 | 国产午夜精品一区二区三区四区 | 成人一级黄色 | 成人免费在线视频播放 | 成人性视频免费网站下载软件 | 日日狠狠久久 | 91精品国产日韩91久久久久久360 | 国产一区二区精品在线观看 | 国产免费中文字幕 | 久久91精品国产91久久yfo | 午夜视频免费播放 | 免费淫视频| 欧美一级小视频 | 精品一区二区久久久久久按摩 | av在线电影网站 | 精品久久中文字幕 | 成年免费网站 | 免费网站看v片在线a | 国产精品视频一区二区三区四 | 久久精品亚洲欧美日韩精品中文字幕 | 久久色伦理资源站 | 九色 在线 | 久久久久久久亚洲视频 | 嗯~啊~弄嗯~啊h高潮视频 | 成人午夜免费网站 | 92看片淫黄大片一级 | 国产精品久久久久久婷婷天堂 | 深夜福利视频免费观看 | 一级毛片手机在线观看 | 国产一级桃视频播放 | 亚洲一级片在线观看 | 欧美日韩一 | 韩国草草影院 | julieann艳星激情办公室 | av电影在线观看免费 | 免费国产一级淫片 | 国产在线看一区 | 成人福利在线看 | 欧美国产一区二区三区激情无套 | 亚洲精品无码不卡在线播放he |