在C#中,WebClient类用于从Web下载数据。以下是使用WebClient类的一些基本步骤:
using System.Net;
using System.IO;
WebClient webClient = new WebClient();
string url = "https://api.example.com/data";
string data = webClient.DownloadString(url);
Console.WriteLine(data);
webClient.Proxy = new WebProxy("proxy.example.com", 8080);
webClient.Credentials = new NetworkCredential("username", "password");
string url = "https://api.example.com/data";
await webClient.DownloadStringTaskAsync(url);
注意:在使用WebClient类时,如果需要频繁访问同一资源,建议使用HttpClient类,因为它具有更好的性能和更丰富的功能。