如何添加 Content-Type: application/octet-stream 到 .Net Core header

How to add Content-Type: application/octet-stream to .Net Core header

我想实现这个,有什么技巧吗

HttpClient c = new HttpClient();
c.DefaultRequestHeaders.Add("Content-type", "application/octet-stream"));

您应该将内容 headers 设置为 HttpContent object,如下所示。

var client = _clientFactory.CreateClient();

using (var content = new ByteArrayContent(byteData))
{
    content.Headers.ContentType =
        new MediaTypeHeaderValue("application/octet-stream");

    var response = await client.PostAsync(uri, content);

    //code logic here

    //...
}

此外,关于使用IHttpClientFactory在ASP.NET Core中创建HttpClient实例的更多信息,请查看:https://docs.microsoft.com/en-us/aspnet/core/fundamentals/http-requests?view=aspnetcore-3.1