c# post formdata
时间: 2023-11-17 07:05:43 浏览: 75
你可以使用 `HttpClient` 类来发送包含 FormData 的 POST 请求。以下是一个示例:
```csharp
using System;
using System.Net.Http;
using System.Threading.Tasks;
class Program
{
static async Task Main(string[] args)
{
using (var httpClient = new HttpClient())
{
var formData = new MultipartFormDataContent();
formData.Add(new StringContent("John"), "username");
formData.Add(new StringContent("Doe"), "lastname");
formData.Add(new StringContent("example@example.com"), "email");
var response = await httpClient.PostAsync("https://example.com/api/user", formData);
if (response.IsSuccessStatusCode)
{
Console.WriteLine("The request was successful.");
}
else
{
Console.WriteLine($"The request failed with status code {response.StatusCode}.");
}
}
}
}
```
在上面的示例中,我们创建了一个包含 FormData 的 `MultipartFormDataContent` 对象,并添加了三个键值对。然后,我们使用 `PostAsync` 方法将其发送到服务器,并检查响应的状态码来确定请求是否成功。
阅读全文