gpt4 book ai didi

c# - ContentDisposition 类抛出不一致的异常

转载 作者:塔克拉玛干 更新时间:2023-11-01 19:09:39 27 4
gpt4 key购买 nike

我正在使用 WebClient 使用内容配置来发现下载文件名。

调用 client.ResponseHeaders["content-disposition"] 返回以下字符串:

attachment; filename="2013122100000030141b0feedd40488fa2b0691fa6ae2a.zip

然后,

fileName = new ContentDisposition(responseHeader).FileName;

抛出异常“指定的内容配置无效。”:

System.FormatException: The specified content disposition is invalid. ---> System.FormatException: The mail header is malformed.
at System.Net.Mime.MailBnfHelper.ReadQuotedString(String data, Int32& offset, StringBuilder builder, Boolean doesntRequireQuotes, Boolean permitUnicodeInDisplayName)
at System.Net.Mime.ContentDisposition.ParseValue()
--- End of inner exception stack trace ---
at System.Net.Mime.ContentDisposition.ParseValue()
at System.Net.Mime.ContentDisposition..ctor(String disposition)

我发现内容配置字符串缺少尾随引号。当引号 (") 放在 .zip 之后时,它起作用。

服务器似乎是 IIS 6。它是 ContentDisposition 类中的错误吗?我不想担心通过删除不需要的引号来处理该字符串,但在这种情况下我无法解释“谁”是错误的,服务器或类。有什么想法吗?

更新:

根据HTTP Content-Disposition specification :

The Content-Disposition response-header field has been proposed as a means for the origin server to suggest a default filename if the user requests that the content is saved to a file. This usage is derived from the definition of Content-Disposition in RFC 1806 [35].

    content-disposition = "Content-Disposition" ":"
disposition-type *( ";" disposition-parm )
disposition-type = "attachment" | disp-extension-token
disposition-parm = filename-parm | disp-extension-parm
filename-parm = "filename" "=" quoted-string
disp-extension-token = token
disp-extension-parm = token "=" ( token | quoted-string ) An example is

Content-Disposition: attachment; filename="fname.ext"

从引用的 RFC 来看,引号不是约束,所以我认为 ContentDisposition 类应该更灵活地接受文件名中不带引号的字符串。

最佳答案

相关的 RFC 是 http://greenbytes.de/tech/webdav/rfc6266.html .不,以引号开头但省略结尾引号是不正确的。

关于c# - ContentDisposition 类抛出不一致的异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20745742/

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