Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unable to get header "Content-Disposition" with httpclient in C#

SCENARIO:
First of all, please consider I'm using HttpClient class, not WebRequest, I know that there is a lot of related question here but all answers are for WebRequest.

I made a Web Api Application that set in this way the following header in order to download a .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 an HttpClient from other application to connect to this method.
Everything is working, retrieves 200 and the content of the file. But I can't get the header in order to read the filename.

TRIES MADE:

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

Content-Disposition: attachment; filename=814Entes_PG_20160114.txt

and from the code I could debug it and I found that the header is in "invalidHeaders" but I can't reach the value:

enter image description here

QUESTION:

How can I set up this and getting without any doubt from the client?

UPDATE:

These are my tries to get the header: The original:

                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();
                }

The second one I tried with some SO investigation:

        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();
                }

            }
like image 988
Leandro Bardelli Avatar asked Jan 14 '16 18:01

Leandro Bardelli


1 Answers

If you are trying to get the headers of the content, it should be from response.Content.Headers

like image 131
terbubbs Avatar answered Oct 12 '22 22:10

terbubbs