[英]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.首先,请考虑我使用的是
HttpClient
class,而不是WebRequest
,我知道这里有很多相关问题,但所有答案都是针对 WebRequest 的。
I made a Web Api Application that set in this way the following header in order to download a.txt file:我制作了一个 Web Api 应用程序,以这种方式设置以下 header 以下载 a.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");
After I made an HttpClient from other application to connect to this method.在我从其他应用程序创建一个 HttpClient 以连接到此方法之后。
Everything is working, retrieves 200 and the content of the file.一切正常,检索 200 和文件的内容。 But I can't get the header in order to read the filename.
但我无法获得 header 来读取文件名。
TRIES MADE:尝试过:
Working with a REST Client I can read the following header attribute:使用 REST 客户端我可以阅读以下 header 属性:
Content-Disposition: attachment;
内容处置:附件; filename=814Entes_PG_20160114.txt
文件名=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:从代码中我可以调试它,我发现 header 在“invalidHeaders”中,但我无法达到该值:
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:这些是我获得 header 的尝试: 原文:
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:第二个我尝试了一些 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();
}
}
如果您尝试获取内容的标题,则应该来自response.Content.Headers
As per @terbubbs you need response.Content.Headers.根据@terbubbs,您需要 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[]否则结果是 System.String[]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.