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类的文档和示例代码。