2010-07-12 36 views

回答

62
string contents; 
using (var wc = new System.Net.WebClient()) 
    contents = wc.DownloadString(url); 
+8

這就是爲什麼我愛的.NET庫.. – 2010-07-12 20:27:06

+1

噢好...我希望這是這麼簡單! – John 2011-07-08 20:15:26

+0

正如@CaffGeek所指​​出的那樣,您會想要在'using'塊中處理'WebClient'。 – TrueWill 2015-07-08 13:23:58

13

使用Web客戶端

var result = string.Empty; 
using (var webClient = new System.Net.WebClient()) 
{ 
    result = webClient.DownloadString("http://some.url"); 
} 
+0

一分鐘太晚了! – 2017-02-26 02:23:10

3

使用這種代碼只需

var r= string.Empty; 
using (var web = new System.Net.WebClient()) 
     r= web.DownloadString("http://TEST.COM"); 
0
using System.IO; 
using System.Net; 

WebClient client = new WebClient(); 

string dnlad = client.DownloadString("http://www.stackoverflow.com/"); 

File.WriteAllText(@"c:\Users\Admin\Desktop\Data1.txt", dnlad); 

得到它從MVA 希望它有助於