在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類,因為它具有更好的性能和更豐富的功能。