c#Base64 字符串通过post发送
时间: 2023-09-30 17:05:54 浏览: 57
你可以使用 C# 中的 `HttpClient` 类来发送 POST 请求并将 Base64 编码的字符串作为请求主体发送。以下是一个示例代码:
```csharp
using System;
using System.Net.Http;
using System.Text;
namespace Base64PostExample
{
class Program
{
static async System.Threading.Tasks.Task Main(string[] args)
{
// Base64 编码的字符串
string base64String = "SGVsbG8gV29ybGQh";
// 创建 HttpClient 对象
using (HttpClient client = new HttpClient())
{
// 设置请求主体
string content = base64String;
StringContent stringContent = new StringContent(content, Encoding.UTF8, "application/x-www-form-urlencoded");
// 发送 POST 请求
HttpResponseMessage response = await client.PostAsync("http://example.com/api", stringContent);
// 处理响应
if (response.IsSuccessStatusCode)
{
Console.WriteLine("请求成功!");
}
else
{
Console.WriteLine("请求失败!");
}
}
}
}
}
```
在上面的代码中,我们创建了一个 `HttpClient` 对象,并使用 `PostAsync` 方法发送 POST 请求。我们将 Base64 编码的字符串作为请求主体,并将其封装在 `StringContent` 对象中。最后,我们检查响应的状态代码以确定请求是否成功。