简体   繁体   English

HttpClient 不返回 Content-Type

[英]HttpClient does not return Content-Type

I'm sending a request with HttpClient .我正在使用HttpClient发送请求。 Server returns two headers which I want return to client.服务器返回我想返回给客户端的两个标头。 I run it like this:我是这样运行的:

 using (var client = new HttpClient())
 {
     var response = await client.GetAsync(DownloadUri + $"?path={path}&fileName={fileName}");
     // ...
 }

But on client side I have 10 headers, while server sends 12. This is what I get in debugger for response.Headers.ToString() :但在客户端我有 10 个标头,而服务器发送 12 个。这是我在调试器中得到的response.Headers.ToString()

Transfer-Encoding: chunked
X-SourceFiles: =?UTF-8?B?QzpcVXNlcnNcQWxleFxEb2N1bWVudHNcdGZzXFVDRktcdnNuXGRldlxMYW5pdC5VQ0ZLLkZpbGUuU2VydmVyXEZpbGUuc3ZjXERvd25sb2Fk?=
Access-Control-Allow-Origin: *
Access-Control-Allow-Methods: POST, GET, OPTIONS
Access-Control-Allow-Credentials: true
Cache-Control: private
Date: Mon, 06 Jun 2016 12:19:09 GMT
Server: Microsoft-IIS/10.0
X-AspNet-Version: 4.0.30319
X-Powered-By: ASP.NET

And this is what I get with external Rest client:这就是我从外部 Rest 客户端得到的: 在此处输入图片说明

Content-Type and Content-Disposition are missing.缺少Content-TypeContent-Disposition How can I get it with HttpClient ?我怎样才能用HttpClient得到它?

You should look at response.Content.Headers you should find headers relating to the content here.您应该查看response.Content.Headers您应该在这里找到与内容相关的标题。 More information about all the content header types can be found on the msdn link below.有关所有内容标头类型的更多信息可以在下面的 msdn 链接中找到。

https://msdn.microsoft.com/en-us/library/system.net.http.headers.httpcontentheaders(v=vs.118).aspx https://msdn.microsoft.com/en-us/library/system.net.http.headers.httpcontentheaders(v=vs.118).aspx

Content_type is part of the Content Headers. Content_type 是内容标题的一部分。 So you should use:所以你应该使用:

response.Content.Headers;
  HttpResponseMessage response = await client.SendAsync(request);
  String sContentType = response.Content.Headers.ContentType.MediaType;
  Console.WriteLine($"Response ContentType: {sContentType}");
  //
  // Response ContentType: application/json

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM