Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

HttpClient does not return Content-Type

I'm sending a request with 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():

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: enter image description here

Content-Type and Content-Disposition are missing. How can I get it with HttpClient?

like image 795
Alex Zhukovskiy Avatar asked Jun 06 '16 12:06

Alex Zhukovskiy


3 Answers

You should look at response.Content.Headers you should find headers relating to the content here. More information about all the content header types can be found on the msdn link below.

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

like image 77
Marc Harry Avatar answered Nov 13 '22 20:11

Marc Harry


Content_type is part of the Content Headers. So you should use:

response.Content.Headers;
like image 4
Mono Avatar answered Nov 13 '22 19:11

Mono


  HttpResponseMessage response = await client.SendAsync(request);
  String sContentType = response.Content.Headers.ContentType.MediaType;
  Console.WriteLine($"Response ContentType: {sContentType}");
  //
  // Response ContentType: application/json
like image 2
nwsmith Avatar answered Nov 13 '22 20:11

nwsmith