是的,你会使用PostAsync方法。
该方法采用一个Uri或一个字符串(就像 WebClient 类中的UploadString方法一样)以及一个HttpContent实例。
该HttpContent实例旨在与不同类型的内容无关,允许您不仅指定提供内容的机制(ByteArrayContent对于字节数组,对于 a 的StreamContentStream等),而且还指定结构(MultipartFormDataContent)。
也就是说,还有一个StringContent类会发送字符串,如下所示:
// 内容。string post = "您要发布的内容";
// The client.
using (client = new HttpClient());
{
// Post.
// Let's assume you're in an async method.
HttpResponseMessage response = await client.Post(
"http://yourdomain/post", new StringContent(post));
// Do something with the response.
}
如果你需要指定一个Encoding,有一个构造函数接受一个Encoding,你可以像这样使用它:
// The client.
using (client = new HttpClient());
{
// Post.
// Let's assume you're in an async method.
HttpResponseMessage response = await client.Post(
"http://yourdomain/post", new StringContent(post),
Encoding.ASCII);
// Do something with the response.
}
从那里开始,在发送响应时处理HttpResponseMessage(如果这对您很重要,如果它不是单向操作)。