WebClient類是C#中用于與Web服務器進行通信的一個工具類。它提供了一組用于發送HTTP請求并接收響應的方法。以下是WebClient類的一些常見用法:
WebClient client = new WebClient();
string response = client.DownloadString("http://example.com/api/data");
Console.WriteLine(response);
WebClient client = new WebClient();
client.Headers.Add("Content-Type", "application/json");
string response = client.UploadString("http://example.com/api/data", "POST", "{\"name\":\"John\",\"age\":30}");
Console.WriteLine(response);
WebClient client = new WebClient();
client.DownloadFile("http://example.com/files/file.txt", "local/path/file.txt");
WebClient client = new WebClient();
client.UploadFile("http://example.com/api/upload", "local/path/file.txt");
WebClient client = new WebClient();
client.Headers.Add("Authorization", "Bearer xxxxxxxxxxxx");
WebClient client = new WebClient();
client.Proxy = new WebProxy("http://proxy.example.com:8080");
這只是WebClient類的一些常見用法,還有其他更多方法和屬性可供使用。根據具體需求,可以進一步探索WebClient類的文檔和示例代碼。