Java 如何编码 URI 参数值?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/444112/
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
How do I encode URI parameter values?
提问by Gili
I want to send a URI as the value of a query/matrix parameter. Before I can append it to an existing URI, I need to encode it according to RFC 2396. For example, given the input:
我想发送一个 URI 作为查询/矩阵参数的值。在将其附加到现有 URI 之前,我需要根据 RFC 2396 对其进行编码。例如,给定输入:
http://google.com/resource?key=value1 & value2
http://google.com/resource?key=value1 & value2
I expect the output:
我期望输出:
http%3a%2f%2fgoogle.com%2fresource%3fkey%3dvalue1%2520%26%2520value2
http%3a%2f%2fgoogle.com%2fresource%3fkey%3dvalue1%2520%26%2520value2
Neither java.net.URLEncoder
nor java.net.URI
will generate the right output. URLEncoder
is meant for HTML form encoding which is not the same as RFC 2396. URI
has no mechanism for encoding a single value at a time so it has no way of knowing that value1 and value2 are part of the same key.
既java.net.URLEncoder
不会也java.net.URI
不会产生正确的输出。URLEncoder
用于 HTML 表单编码,与 RFC 2396 不同。URI
没有一次编码单个值的机制,因此无法知道 value1 和 value2 是同一键的一部分。
采纳答案by Peter ?tibrany
Jersey's UriBuilderencodes URI components using application/x-www-form-urlencoded and RFC 3986 as needed. According to the Javadoc
Jersey 的UriBuilder根据需要使用 application/x-www-form-urlencoded 和 RFC 3986 对 URI 组件进行编码。根据 Javadoc
Builder methods perform contextual encoding of characters not permitted in the corresponding URI component following the rules of the application/x-www-form-urlencoded media type for query parameters and RFC 3986 for all other components. Note that only characters not permitted in a particular component are subject to encoding so, e.g., a path supplied to one of the path methods may contain matrix parameters or multiple path segments since the separators are legal characters and will not be encoded. Percent encoded values are also recognized where allowed and will not be double encoded.
构建器方法按照 application/x-www-form-urlencoded 媒体类型的规则对查询参数和 RFC 3986 的所有其他组件执行相应 URI 组件中不允许的字符的上下文编码。请注意,只有在特定组件中不允许的字符才需要进行编码,例如,提供给其中一种路径方法的路径可能包含矩阵参数或多个路径段,因为分隔符是合法字符并且不会被编码。在允许的情况下也会识别百分比编码值,并且不会进行双重编码。
回答by Peter ?tibrany
It seems that CharEscapersfrom Google GData-java-client has what you want. It has uriPathEscaper method, uriQueryStringEscaper, and generic uriEscaper. (All return Escaper object which does actual escaping). Apache License.
似乎来自 Google GData-java-client 的CharEscapers有你想要的。它有 uriPathEscaper 方法、uriQueryStringEscaper 和通用 uriEscaper。(所有返回执行实际转义的 Escaper 对象)。阿帕奇许可证。
回答by OscarRyz
Mmhh I know you've already discarded URLEncoder, but despite of what the docs say, I decided to give it a try.
嗯,我知道你已经放弃了 URLEncoder,但不管文档怎么说,我还是决定试一试。
You said:
你说:
For example, given an input:
http://google.com/resource?key=value
I expect the output:
http%3a%2f%2fgoogle.com%2fresource%3fkey%3dvalue
例如,给定一个输入:
http://google.com/resource?key=value
我期望输出:
http%3a%2f%2fgoogle.com%2fresource%3fkey%3dvalue
So:
所以:
C:\oreyes\samples\java\URL>type URLEncodeSample.java
import java.net.*;
public class URLEncodeSample {
public static void main( String [] args ) throws Throwable {
System.out.println( URLEncoder.encode( args[0], "UTF-8" ));
}
}
C:\oreyes\samples\java\URL>javac URLEncodeSample.java
C:\oreyes\samples\java\URL>java URLEncodeSample "http://google.com/resource?key=value"
http%3A%2F%2Fgoogle.com%2Fresource%3Fkey%3Dvalue
As expected.
正如预期的那样。
What would be the problem with this?
这会有什么问题?
回答by yincrash
I don't have enough reputation to comment on answers, but I just wanted to note that downloading the JSR-311 api by itself will not work. You need to download the reference implementation (jersey).
我没有足够的声誉来评论答案,但我只想指出,单独下载 JSR-311 api 是行不通的。您需要下载参考实现 ( jersey)。
Only downloading the api from the JSR page will give you a ClassNotFoundException when the api tries to look for an implementation at runtime.
当 api 尝试在运行时查找实现时,只有从 JSR 页面下载 api 才会给您一个 ClassNotFoundException。
回答by Daniel Murphy
I wrote my own, it's short, super simple, and you can copy it if you like: http://www.dmurph.com/2011/01/java-uri-encoder/
我自己写的,很短,超级简单,喜欢的可以复制一下:http: //www.dmurph.com/2011/01/java-uri-encoder/