- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
场景:
首先,请考虑我使用的是 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”中,但我无法达到该值:
问题:
我怎样才能设置这个并毫无疑问地从客户那里得到?
更新:
这些是我获取标题的尝试:原文:
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/
我在尝试在 IE8 中下载带有分号的文件名时遇到问题。 Response.AddHeader("Content-Disposition", "attachment; filename=\"" + at
我正在尝试将多个文件附加到电子邮件中。 除了文本文件缺少第一行之外,它工作正常。 注意:为了可读性,删除了所有错误处理。此外,假设正确设置了收件人/发件人/主题等(电子邮件发送完美 - 除了附件问题)
我正在努力使用 java spring 创建对某些内部服务的有效请求。问题在于多部分/表单数据边界的正确负载。 环境:java 服务器 -> (其余)http multipart/form-data
我可以通过添加以下内容来设置请求 header 以公开 Content-Disposition: “访问控制公开 header ”:“内容处置” 我可以看到响应,但响应对象不包括 Content-Di
我对附件名称有疑问。当我在 google chrome 上调用该站点时,它会返回具有正确名称和扩展名的文件。我用 Internet Explorer 对其进行了测试,它也可以正常工作。问题仅在于 Fi
我在htaccess文件中有了以下规则,以强制链接文件下载而不是在浏览器中打开: ForceType application/octet-stream Header set Content-
我正在构建一个网站,该网站通过 javascript 与接受发布的 json 请求的网络服务进行通信。我使用 XMLHttpRequest 发布这些请求。在某些情况下,服务会返回下载响应: 例子: C
我有一些文件,我希望人们能够根据需要在浏览器中下载或查看这些文件。我不知道如何简单地做到这一点,在主机中设置 header 或做一些 javascript 魔术。 最终结果应该是一个 html 页面,
我正在尝试将图片文件从我的 Android 应用程序上传到我的 JavaEE REST 服务,该服务已部署到 JBoss Wildfly 9 服务器。 我对 Content-Disposition 的
除了从未设置内容配置 header 和下载的文件名称不正确之外,以下代码运行良好。 我搜索了 Java API,但找不到设置 header 的方法。我试图在应用函数中设置它,但此时响应对象为空。我尝试
我一直在尝试下载带有中文文件名的附件,但不知何故它们的编码在下载时发生了变化,并且在有中文字符的地方保存了一些乱码文件名。 技术:Java服务器:Apache Tomcat 这是我已经尝试过的 res
我很难找到一个问题的解决方案,我认为这个问题很常见或很容易解决,但显然我错了。 我必须重新编写一些导致问题的网络代码(不是我写的)。当用户单击链接时,请求会发送到 Web 服务器,而 Web 服务器又
我正在将文件名传递给下载页面。 即 somefile.xls 下载页面将完整的目录路径添加回文件名。 即 c:\temp\somefile.xls 问题是现在设置 header 的“Content-D
我正在使用 curl -w "%%{filename_effective}" "http://example.com/file.jpg" -L -J -O -s 从下载地址打印服务器强加的文件名,但它
我正在尝试下载 Struts Action 类中的 PDF 文件。问题是使用 response.setHeader("Content-Disposition", "attachment;filenam
我有一个 php add,它调用 LaTeX,然后将 PDF 传递给浏览器。由于我的用户将为此服务付费,我想确保他们可以选择保存 PDF,而不是一次又一次地访问我的服务器。 exec("cd tex
如何使用 WebClient 获取从 WebAPI Controller 返回的 Content-Disposition 参数? WebAPI Controller [Route("api/m
关闭。这个问题需要details or clarity .它目前不接受答案。 想改进这个问题吗? 通过 editing this post 添加细节并澄清问题. 关闭 8 年前。 Improve t
几天来我一直在寻找这个问题的解决方案,但找不到。 我想使用网络客户端从网络服务器下载文件。下载工作正常,但我无法获得真实的文件名,这对我来说非常重要。 我在很多主页上看到,文件名应该保存在 Conte
我想强制浏览器下载一个 pdf 文件。 我正在使用以下代码: Click here to Download quotation 它使浏览器在新窗口中打开 pdf,但我希望在用户单击它时将其下载到硬盘。
我是一名优秀的程序员,十分优秀!