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資源的請求進行身份驗證的網絡憑據。
Byte[] pageData = MyWebClient.DownloadData("從指定網站下載數據
string pageHtml = Encoding.Default.GetString(pageData); //如果獲取網站頁面采用的是GB2312,則使用這句
//string pageHtml = Encoding.UTF8.GetString(pageData); //如果獲取網站頁面采用的是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());
}
}
}
}
注:內容轉自菩提樹下的楊過的博客
新聞熱點
疑難解答