string 在查询字符串值中使用的分隔符
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/642758/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Delimiter to use within a query string value
提问by Matthew Cole
I need to accept a list of file names in a query string. ie:
我需要在查询字符串中接受文件名列表。IE:
http://someSite/someApp/myUtil.ashx?files=file1.txt|file2.bmp|file3.doc
Do you have any recommendations on what delimiter to use?
您对使用什么分隔符有什么建议吗?
采纳答案by MSalters
If they're filenames, a good choice would be a character which is disallowed in filenames. Suggestions so far included , | &
which are generally allowed in filenames and therefore might lead to ambiguities. /
on the other hand is generally not allowed, not even on Windows. It is allowed in URIs, and it has no special meaning in query strings.
如果它们是文件名,一个不错的选择是文件名中不允许使用的字符。目前包含的建议, | &
通常在文件名中是允许的,因此可能会导致歧义。/
另一方面,通常是不允许的,甚至在 Windows 上也不行。它在 URI 中是允许的,它在查询字符串中没有特殊含义。
Example:
例子:
http://someSite/someApp/myUtil.ashx?files=file1.txt|file2.bmp|file3.doc
is bad because it may refer to the valid file file1.txt|file2.bmp
.
http://someSite/someApp/myUtil.ashx?files=file1.txt|file2.bmp|file3.doc
不好,因为它可能引用有效文件file1.txt|file2.bmp
。
http://someSite/someApp/myUtil.ashx?files=file1.txt/file2.bmp/file3.doc
unambiguously refers to 3 files.
http://someSite/someApp/myUtil.ashx?files=file1.txt/file2.bmp/file3.doc
明确指的是 3 个文件。
回答by Cory Kendall
Having query parameters multiple times is legal, and the only way to guarantee no parsing problems in all cases:
多次查询参数是合法的,并且是保证在所有情况下都没有解析问题的唯一方法:
http://someSite/someApp/myUtil.ashx?file=file1.txt&file=file2.bmp&file=file3.doc
The semicolon ;
must be URI encoded if part of a filename (turned to %3B
), yet not if it is separating query parameters which is its reserved use.
如果分号;
是文件名的一部分(变成%3B
),则分号必须是 URI 编码的,但如果它分隔查询参数,这是它的保留用途,则不是。
See section 2.2 of this rfc:
请参阅此 rfc 的第 2.2 节:
2.2. Reserved Characters
URIs include components and subcomponents that are delimited by characters in the "reserved" set. These characters are called "reserved" because they may (or may not) be defined as delimiters by the generic syntax, by each scheme-specific syntax, or by the implementation-specific syntax of a URI's dereferencing algorithm. If data for a URI component would conflict with a reserved character's purpose as a delimiter, then the conflicting data must be percent-encoded before the URI is formed.
reserved = gen-delims / sub-delims gen-delims = ":" / "/" / "?" / "#" / "[" / "]" / "@" sub-delims = "!" / "$" / "&" / "'" / "(" / ")" / "*" / "+" / "," / ";" / "="
2.2. 保留字符
URI 包括由“保留”集中的字符分隔的组件和子组件。这些字符被称为“保留”,因为它们可能(或可能不会)被通用语法、每个方案特定的语法或 URI 解引用算法的实现特定的语法定义为定界符。如果 URI 组件的数据与作为分隔符的保留字符的用途发生冲突,则必须在形成 URI 之前对冲突数据进行百分比编码。
reserved = gen-delims / sub-delims gen-delims = ":" / "/" / "?" / "#" / "[" / "]" / "@" sub-delims = "!" / "$" / "&" / "'" / "(" / ")" / "*" / "+" / "," / ";" / "="
回答by Andrew Harry
I would recommend making each file its own query parameter, i.e.
我建议让每个文件都有自己的查询参数,即
myUtil.ashx?file1=file1.txt&file2=file2.bmp&file3=file3.doc
This way you can just use standard query parsing and loop
这样你就可以只使用标准的查询解析和循环
回答by Jimmy Stenke
Do you need to list the filenames as a string? Most languages accepts arrays in the querystring so you could write it like
您是否需要将文件名作为字符串列出?大多数语言都接受查询字符串中的数组,因此您可以将其编写为
http://someSite/someApp/myUtil.ashx?files[]=file1.txt&files[]=file2.bmp&files[]=file3.doc
If it doesn't, or you can't use for some other reason, you should stick to a delimiter that is either not allowed or unusual in a filename. Pipe (|) is a good one, otherwise you could urlencode an invisible character since they are quite easy to use in coding, but harder to actually include in a filename.
如果没有,或者由于其他原因无法使用,则应坚持使用文件名中不允许或不常见的分隔符。管道 (|) 是一个很好的选择,否则你可以对一个不可见的字符进行 urlencode,因为它们在编码中很容易使用,但实际上更难包含在文件名中。
I usually use arrays when possible and pipe otherwise.
我通常在可能的情况下使用数组,否则使用管道。
回答by Rick Hochstetler
I've always used double pipes "||". I don't have any good evidence to back up why this is a good choice other than 10 years of web programming and it's never been an issue.
我一直使用双管“||”。除了 10 年的 Web 编程之外,我没有任何好的证据来支持为什么这是一个不错的选择,而且这从来都不是问题。
回答by Shawn Kovac
This is one common problem. How i handled it was: I created a method which accepted a list of strings, then found a character that was not in any of the strings. (I did this by a simple concatenation of the strings, then testing for various characters.) Once a character was found, concatenated all the strings together but also prepended the string with the separation character. So in the given question, one example wud be: http://someSite/someApp/myUtil.ashx?files=|file1.txt|file2.bmp|file3.docand another wud be: http://someSite/someApp/myUtil.ashx?files=,file1.txt,file2.bmp,file3.docBut since i actually use a method that guarantees my separator character is not in the rest of the strings, it is safe. It was a bit of work to create the first time, but i've used it MANY times in various applications.
这是一个常见问题。我的处理方式是:我创建了一个接受字符串列表的方法,然后找到了一个不在任何字符串中的字符。(我通过简单地串联字符串,然后测试各种字符来做到这一点。)一旦找到一个字符,就将所有字符串串联在一起,但也要在字符串前面加上分隔字符。所以在给定的问题中,一个例子是: http://someSite/someApp/myUtil.ashx?files=|file1.txt|file2.bmp|file3.doc另一个例子是: http://someSite/someApp/ myUtil.ashx?files=,file1.txt,file2.bmp,file3.doc但是因为我实际上使用了一种方法来保证我的分隔符不在字符串的其余部分中,所以它是安全的。第一次创建需要一些工作,但我已经在各种应用程序中多次使用它。
回答by timodius
I would build on MSalters answer by saying, to generalize, the best delimiter is one that is invalid to the items in the list. For example, if your list is prices, a comma is a bad delimiter because it can be confused with the values. For that reason, as most these answers suggest, I think a good general purpose delimiter is probably "|" as it is rarely a valid value. "/" is maybe not the best delimiter generally as it is valid for paths sometimes.
我会基于 MSalters 的回答说,概括地说,最好的分隔符是对列表中的项目无效的分隔符。例如,如果您的列表是价格,逗号是一个不好的分隔符,因为它可能与值混淆。出于这个原因,正如大多数这些答案所暗示的那样,我认为一个好的通用分隔符可能是“|” 因为它很少是有效值。“/”通常可能不是最好的分隔符,因为它有时对路径有效。
回答by nan
I think I would consider using commas or semicolons.
我想我会考虑使用逗号或分号。