Failed to get "Content-Disposition" header using httpclient in C #

SCENARIO:
First of all, consider that I am using a class HttpClient, not WebRequest. I know there are many related questions here, but all the answers are for WebRequest.

I made a Web Api application that set the following header in this way to download the .txt file:

resp.Content = new StringContent(result, System.Text.Encoding.UTF8, "text/plain");
resp.Content.Headers.ContentDisposition = new System.Net.Http.Headers.ContentDispositionHeaderValue("attachment")
{
    FileName = _datacontext.GetAppConfig("814FileNameHeader") + DateTime.Now.ToString("yyyyMMdd") + ".txt"
};
resp.Content.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream");  

After I made HttpClient from another application to connect to this method.
Everything works, extracts 200 and the contents of the file. But I can not get the header to read the file name.

BROWSE:

Working with a REST Client I can read the following header attribute:

Content-Disposition: attachment; file name = 814Entes_PG_20160114.txt

, , "invalidHeaders", :

enter image description here

:

- ?

UPDATE:

: :

                using (var httpClient = new HttpClient())
            {
                httpClient.BaseAddress = new Uri(uri);
                string authentication = string.Concat(authenticationArgs[0], ":", authenticationArgs[1]);
                httpClient.DefaultRequestHeaders.TryAddWithoutValidation("Authorization", "Basic " + Base64Encode(authentication));
                httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
                HttpRequestMessage req = new HttpRequestMessage(HttpMethod.Post, "Report/" + type);
                req.Content = new StringContent("");
                HttpResponseMessage response = await httpClient.SendAsync(req);
                Console.WriteLine(response.StatusCode);
                if (response.IsSuccessStatusCode)
                {
                    string stream = response.Content.ReadAsStringAsync().Result;
                    Console.Write("Respuesta servicio: " + stream);
                    Console.WriteLine(stream);

                  string cp =  response.Headers.GetValues("Content-Disposition").ToString();
                }

, SO:

        using (var httpClient = new HttpClient())
        {
            httpClient.BaseAddress = new Uri(uri);
            string authentication = string.Concat(authenticationArgs[0], ":", authenticationArgs[1]);
            httpClient.DefaultRequestHeaders.TryAddWithoutValidation("Authorization", "Basic " + Base64Encode(authentication));
            httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
            HttpRequestMessage req = new HttpRequestMessage(HttpMethod.Post, "Report/" + type);
            req.Content = new StringContent("");
            HttpResponseMessage response = await httpClient.SendAsync(req);
            Console.WriteLine(response.StatusCode);
            if (response.IsSuccessStatusCode)
            {
                string stream = response.Content.ReadAsStringAsync().Result;
                Console.Write("Respuesta servicio: " + stream);
                Console.WriteLine(stream);
                string cp = "";
                HttpHeaders headers = response.Headers;
                IEnumerable<string> values;
                if (headers.TryGetValues("Content-Disposition", out values))
                {
                    cp = values.ToString();
                }

            }
+4
1

, response.Content.Headers

+9

Source: https://habr.com/ru/post/1624330/


All Articles