c# WebClient
时间: 2023-12-13 19:33:50 浏览: 145
C#中的WebClient类是一个用于发送HTTP请求并接收HTTP响应的类。它可以用于下载文件、上传文件、发送POST和GET请求等。以下是WebClient类的一些用法实例:
1. 下载文件
```csharp
WebClient webClient = new WebClient();
webClient.DownloadFile("http://example.com/file.txt", "file.txt");
```
2. 下载字符串
```csharp
WebClient webClient = new WebClient();
webClient.Encoding = Encoding.UTF8; // 设置编码格式
string result = webClient.DownloadString("http://example.com");
```
3. 上传文件
```csharp
WebClient webClient = new WebClient();
webClient.UploadFile("http://example.com/upload", "file.txt");
```
4. 发送POST请求
```csharp
WebClient webClient = new WebClient();
webClient.Headers[HttpRequestHeader.ContentType] = "application/x-www-form-urlencoded";
string result = webClient.UploadString("http://example.com/post", "name=value");
```
阅读全文