//var url = "http://demo.com/api/streamcontent";
//var item =new MyClass();
HttpClient httpClient = new HttpClient();
using (HttpRequestMessage request = new HttpRequestMessage())
{
request.Method = HttpMethod.Post;
request.RequestUri = new Uri(url);
using (MemoryStream ms = new MemoryStream())
{
//使用二进制序列化将你需要转送的对象转换成Stream
BinaryFormatter binaryFormatter = new BinaryFormatter();
binaryFormatter.Serialize(ms, item);
ms.Position = 0; //重置ms的position,便于StreamContent读取流内容
request.Content = new StreamContent(ms, (int)ms.Length);
var res = httpClient.SendAsync(request).Result;
res.EnsureSuccessStatusCode();
//处理返回结果
}
}
C# HttpClient StreamContent用法
最新推荐文章于 2023-10-29 08:48:23 发布