【问题标题】:How to add Content-Type: application/octet-stream to .Net Core header如何将 Content-Type: application/octet-stream 添加到 .Net Core 标头
【发布时间】:2020-04-21 18:04:45
【问题描述】:

我要完成这个,有什么诀窍吗

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

【问题讨论】:

    标签: asp.net-core .net-core dotnet-httpclient


    【解决方案1】:

    您应该使用HttpContent 对象设置内容标题,如下所示。

    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
    
        //...
    }
    

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-09-11
      • 1970-01-01
      • 2011-01-26
      • 2012-06-25
      • 2011-10-04
      • 2017-11-03
      • 1970-01-01
      相关资源
      最近更新 更多