using system;
using system.collections.generic;
using system.text;
using system.net;
using system.io;
namespace thief
{
class program
{
static void main(string[] args)
{
try {
webclient mywebclient = new webclient();
mywebclient.credentials = credentialcache.defaultcredentials;//獲取或設置用于對向internet資源的請求進行身份驗證的網(wǎng)絡憑據(jù)。
byte[] pagedata = mywebclient.downloaddata("http://www.163.com");//從指定網(wǎng)站下載數(shù)據(jù)
string pagehtml = encoding.default.getstring(pagedata); //如果獲取網(wǎng)站頁面采用的是gb2312,則使用這句
//string pagehtml = encoding.utf8.getstring(pagedata); //如果獲取網(wǎng)站頁面采用的是utf-8,則使用這句
console.writeline(pagehtml);//在控制臺輸入獲取的內容
using (streamwriter sw = new streamwriter("c://test//ouput.html"))//將獲取的內容寫入文本
{
sw.write(pagehtml);
}
console.readline(); //讓控制臺暫停,否則一閃而過了
}
catch(webexception webex) {
console.writeline(webex.message.tostring());
}
}
}
}
注:內容轉自菩提樹下的楊過的博客
新聞熱點
疑難解答