gpt4 book ai didi

c# - 无法在 C# 中使用 httpclient 获取 header "Content-Disposition"

转载 作者:太空狗 更新时间:2023-10-30 00:18:32 31 4
gpt4 key购买 nike

场景:
首先,请考虑我使用的是 HttpClient 类,而不是 WebRequest,我知道这里有很多相关问题,但所有答案都是针对 WebRequest 的。

我制作了一个 Web Api 应用程序,它以这种方式设置了以下 header ,以便下载 .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");

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

所做的尝试:

使用 REST 客户端我可以读取以下 header 属性:

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

从代码中我可以调试它,我发现 header 在“invalidHeaders”中,但我无法达到该值:

enter image description here

问题:

我怎样才能设置这个并毫无疑问地从客户那里得到?

更新:

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

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

}

最佳答案

如果你想获取内容的标题,它应该来自response.Content.Headers

关于c# - 无法在 C# 中使用 httpclient 获取 header "Content-Disposition",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34797229/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com