如何对 URI 参数值进行编码?

发布于 2024-07-12 00:12:00 字数 447 浏览 6 评论 0原文

我想发送 URI 作为查询/矩阵参数的值。 在将其附加到现有 URI 之前,我需要根据 RFC 2396 对其进行编码。例如,给定输入:

http://google.com/resource?key=value1 & value2

我期望输出:

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

两者都不是java.net.URLEncoder 和 java.net.URI 都不会生成正确的输出。 URLEncoder 用于 HTML 表单编码,与 RFC 2396 不同。URI 没有一次编码单个值的机制,因此它无法知道value1 和 value2 是同一键的一部分。

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:

http://google.com/resource?key=value1 & value2

I expect the output:

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

往事风中埋 2024-07-19 00:12:00

Jersey 的 UriBuilder 使用应用程序对 URI 组件进行编码/x-www-form-urlencoded 和 RFC 3986(根据需要)。 根据 Javadoc

Builder 方法按照查询参数的 application/x-www-form-urlencoded 媒体类型规则和所有其他组件的 RFC 3986 规则,对相应 URI 组件中不允许的字符执行上下文编码。 请注意,只有特定组件中不允许的字符才会受到编码,因此,例如,提供给路径方法之一的路径可能包含矩阵参数或多个路径段,因为分隔符是合法字符并且不会被编码。 在允许的情况下,百分比编码值也会被识别,并且不会被双重编码。

Jersey's UriBuilder encodes URI components using application/x-www-form-urlencoded and RFC 3986 as needed. According to the 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.

一江春梦 2024-07-19 00:12:00

您还可以使用 Spring 的 UriUtils

You could also use Spring's UriUtils

猫瑾少女 2024-07-19 00:12:00

我没有足够的声誉来评论答案,但我只是想指出,单独下载 JSR-311 api 是行不通的。 您需要下载参考实现 (jersey)。

当 api 尝试在运行时查找实现时,仅从 JSR 页面下载 api 才会出现 ClassNotFoundException。

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).

Only downloading the api from the JSR page will give you a ClassNotFoundException when the api tries to look for an implementation at runtime.

許願樹丅啲祈禱 2024-07-19 00:12:00

我自己写的,很短,超级简单,如果你愿意,你可以复制它:
http://www.dmurph.com/2011/01/java-uri-编码器/

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/

金橙橙 2024-07-19 00:12:00

似乎 CharEscapers 来自 Google GData-java-client 有你想要的。 它有 uriPathEscaper 方法、uriQueryStringEscaper 和通用 uriEscaper。 (所有返回执行实际转义的 Escaper 对象)。 阿帕奇许可证。

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.

汐鸠 2024-07-19 00:12:00

我认为 URI 类就是您正在寻找的那一位。

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

傲娇萝莉攻 2024-07-19 00:12:00

嗯,我知道你已经放弃了 URLEncoder,但不管文档怎么说,我还是决定尝试一下。

你说:

例如,给定一个输入:

http://google.com/resource?key=value< /em>

我期望的输出:

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

所以:

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

正如预期的那样。

这会出现什么问题呢?

Mmhh I know you've already discarded URLEncoder, but despite of what the docs say, I decided to give it a try.

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

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?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文