HttpClient没有返回Content-Type

7

我正在使用HttpClient发送请求。服务器返回两个头文件,我希望将它们返回给客户端。我这样运行它:

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

但是在客户端我有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

这是我使用外部 Rest 客户端得到的结果: enter image description here Content-TypeContent-Disposition 缺失了。如何使用 HttpClient 获取它们?

1
您没有发送任何Accept-Headers,因此可能不会有压缩(但是原始的)响应,因此也没有指定的内容类型。 - CSharpie
纠正重复的回答:https://dev59.com/C2Qo5IYBdhLWcg3wGsPN#41836921 - Alexei Levenkov
3个回答

13

4

Content_type是Content Headers的一部分。因此,您应该使用:

response.Content.Headers;

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

请解释一下你的代码是做什么的,以及它与其他答案有何不同/更好之处。 - BDL
1
这是一个完整的特定C#代码示例,用于将ContentType返回为字符串。它展示了当执行代码时输出的示例(在注释行中)。因此,如果这就是你需要的,你就不必进一步研究这个主题。其他答案只是指向了答案。我相信,对于任何对C#和相关主题有一定了解的人来说,这段代码都很容易理解。 - nwsmith

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接