简体   繁体   中英

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:

在此处输入图像描述

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

            }

如果您尝试获取内容的标题,则应该来自response.Content.Headers

As per @terbubbs you need response.Content.Headers.

But also code needs to be - for the first code sample:

string cp = response.Result.Content.Headers.GetValues("Content-Disposition").ToList()[0];

And for the second:

cp = values.FirstOrDefault().ToString();

Otherwise the result is System.String[]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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