如何编码URI参数值?

时间:2021-12-10 07:33:05

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?= value1 & value2的关键

I expect the output:

我期望的输出:

http%3a%2f%2fgoogle.com%2fresource%3fkey%3dvalue1%2520%26%2520value2

http % 3 a % 2 f % 2 fgoogle.com % 2 fresource % 3 fkey % 3 dvalue1 % 2520% 26% 2520 value2

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是同一个键的一部分。

7 个解决方案

#1


30  

Jersey's UriBuilder encodes URI components using application/x-www-form-urlencoded and RFC 3986 as needed. According to the Javadoc

Jersey的UriBuilder使用application/ www-form- urlencoding和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.

构建器方法按照应用程序/x-www-form- urlencoding media类型的规则对相应URI组件中不允许的字符执行上下文编码,对所有其他组件执行RFC 3986。注意,只有在特定组件中不允许的字符才需要进行编码,因此,例如,提供给路径方法之一的路径可能包含矩阵参数或多个路径段,因为分隔符是合法字符,不会被编码。百分比编码的值也在允许的地方被识别,不会被双编码。

#2


17  

You could also use Spring's UriUtils

您还可以使用Spring的UriUtils

#3


9  

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。

#4


4  

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 http://www.dmurph /2011/01/java-uri-encoder/

#5


2  

It seems that CharEscapers from 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.

看起来来自谷歌GData-java-client的CharEscapers具有您想要的功能。它有uriPathEscaper方法、uriQueryStringEscaper方法和通用的uriEscaper方法。(所有返回的逃避对象,它确实会逃跑)。Apache许可证。

#6


-2  

I think that the URI class is the one that you are looking for.

我认为URI类是您正在寻找的。

#7


-3  

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

http://google.com/resource?key=value

I expect the output:

我期望的输出:

http%3a%2f%2fgoogle.com%2fresource%3fkey%3dvalue

http % 3 a % 2 f % 2 fgoogle.com % 2 fresource % 3 fkey dvalue % 3

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?

这有什么问题呢?

#1


30  

Jersey's UriBuilder encodes URI components using application/x-www-form-urlencoded and RFC 3986 as needed. According to the Javadoc

Jersey的UriBuilder使用application/ www-form- urlencoding和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.

构建器方法按照应用程序/x-www-form- urlencoding media类型的规则对相应URI组件中不允许的字符执行上下文编码,对所有其他组件执行RFC 3986。注意,只有在特定组件中不允许的字符才需要进行编码,因此,例如,提供给路径方法之一的路径可能包含矩阵参数或多个路径段,因为分隔符是合法字符,不会被编码。百分比编码的值也在允许的地方被识别,不会被双编码。

#2


17  

You could also use Spring's UriUtils

您还可以使用Spring的UriUtils

#3


9  

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。

#4


4  

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 http://www.dmurph /2011/01/java-uri-encoder/

#5


2  

It seems that CharEscapers from 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.

看起来来自谷歌GData-java-client的CharEscapers具有您想要的功能。它有uriPathEscaper方法、uriQueryStringEscaper方法和通用的uriEscaper方法。(所有返回的逃避对象,它确实会逃跑)。Apache许可证。

#6


-2  

I think that the URI class is the one that you are looking for.

我认为URI类是您正在寻找的。

#7


-3  

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

http://google.com/resource?key=value

I expect the output:

我期望的输出:

http%3a%2f%2fgoogle.com%2fresource%3fkey%3dvalue

http % 3 a % 2 f % 2 fgoogle.com % 2 fresource % 3 fkey dvalue % 3

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?

这有什么问题呢?