C#下载网络资源(网页或文件)
var wc = new WebClient(); //下载网络资源到文件 wc.DownloadFile("http://www.baidu.com/img/PCtm_d9c8750bed0b3c7d089fa7d55720d6cf.png", "d:\\1.png"); //下载网络资源到内存 var bytes = wc.DownloadData("http://www.baidu.com/img/PCtm_d9c8750bed0b3c7d089fa7d55720d6cf.png"); //下载网络资源到字符串
wc.Encoding = System.Text.Encoding.UTF8;
var html = wc.DownloadString("http://www.baidu.com");
WebClient 已过时,使用 HttpClient:
var client = new HttpClient(); var response = await client.GetAsync("https://xxxx/master.html"); if (response.IsSuccessStatusCode) { var content = await response.Content.ReadAsStringAsync(); File.WriteAllText(@"C:\Users\...\Desktop\test.txt", content); } else { Console.WriteLine($"Error: {response.StatusCode}"); }
桂棹兮兰桨,击空明兮溯流光。