无法在C#中使用httpclient获取标题"Content-Disposition"

Lea*_*dro 4 .net c# attachment http-headers dotnet-httpclient

情景:
首先,请考虑我正在使用HttpClient课程,不是WebRequest,我知道这里有很多相关问题,但所有答案都是针对WebRequest的.

我制作了一个Web Api应用程序,以这种方式设置以下标题,以便下载.txt文件:

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");  
Run Code Online (Sandbox Code Playgroud)

之后我从其他应用程序中创建了一个HttpClient来连接到这个方法.
一切正常,检索200和文件的内容.但我无法获取标题以读取文件名.

TRIES MADE:

使用REST客户端我可以阅读以下标头属性:

内容 - 处理:附件; 文件名= 814Entes_PG_20160114.txt

从代码我可以调试它,我发现标题是在"invalidHeaders",但我无法达到值:

在此输入图像描述

题:

我如何设置这个并毫无疑问地从客户那里获得?

更新:

这些是我尝试获取标题:原始:

                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();
                }
Run Code Online (Sandbox Code Playgroud)

第二个我尝试了一些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();
                }

            }
Run Code Online (Sandbox Code Playgroud)

ter*_*bbs 11

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