用Java编码URL查询参数

发布于 2024-10-22 01:35:22 字数 448 浏览 2 评论 0 原文

如何在 Java 中对 url 上的查询参数进行编码?我知道,这似乎是一个显而易见且已经被问过的问题。

有两个微妙之处我不确定:

  1. url 上的空格应该编码为“+”还是“%20”?在 chrome 中,如果我输入“http://google.com/foo=?bar me”,chrome 会将其更改为使用 %20 进行编码,
  2. 是否有必要/正确地将冒号“:”编码为 %3B? Chrome 没有。

注意:

  • java.net.URLEncoder.encode 似乎不起作用,它似乎是为了对表单提交的数据进行编码。例如,它将空格编码为 + 而不是 %20,并对不必要的冒号进行编码。
  • java.net.URI 不编码查询参数

How does one encode query parameters to go on a url in Java? I know, this seems like an obvious and already asked question.

There are two subtleties I'm not sure of:

  1. Should spaces be encoded on the url as "+" or as "%20"? In chrome if I type in "http://google.com/foo=?bar me" chrome changes it to be encoded with %20
  2. Is it necessary/correct to encode colons ":" as %3B? Chrome doesn't.

Notes:

  • java.net.URLEncoder.encode doesn't seem to work, it seems to be for encoding data to be form submitted. For example, it encodes space as + instead of %20, and encodes colon which isn't necessary.
  • java.net.URI doesn't encode query parameters

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

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

发布评论

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

评论(9

北城半夏 2024-10-29 01:35:22

java.net.URLEncoder.encode(String s, String编码) 也可以提供帮助。它遵循 HTML 表单编码 application/x-www-form-urlencoded

URLEncoder.encode(query, "UTF-8");

另一方面,百分比编码(也称为URL 编码) 使用 %20 对空格进行编码。冒号是保留字符,因此编码后 : 仍将保留冒号。

java.net.URLEncoder.encode(String s, String encoding) can help too. It follows the HTML form encoding application/x-www-form-urlencoded.

URLEncoder.encode(query, "UTF-8");

On the other hand, Percent-encoding (also known as URL encoding) encodes space with %20. Colon is a reserved character, so : will still remain a colon, after encoding.

野却迷人 2024-10-29 01:35:22

不幸的是, URLEncoder.encode() 不会产生有效的百分比编码(如 中指定的) RFC 3986)。

URLEncoder.encode() 可以很好地对所有内容进行编码,除了空格被编码为“+”。我能找到的所有 Java URI 编码器都只公开公共方法来编码查询、片段、路径部分等,但不公开“原始”编码。不幸的是,因为片段和查询允许将空格编码为 +,所以我们不想使用它们。路径已正确编码,但首先“标准化”,因此我们也不能将其用于“通用”编码。

我能想到的最佳解决方案:

return URLEncoder.encode(raw, StandardCharsets.UTF_8).replaceAll("\\+", "%20");

如果 replaceAll() 对你来说太慢,我想替代方案是滚动你自己的编码器...

编辑:我首先在这里有这个代码,它不会t 正确编码“?”、“&”、“=”:

//don't use - doesn't properly encode "?", "&", "="
new URI(null, null, null, raw, null).toString().substring(1);

Unfortunately, URLEncoder.encode() does not produce valid percent-encoding (as specified in RFC 3986).

URLEncoder.encode() encodes everything just fine, except space is encoded to "+". All the Java URI encoders that I could find only expose public methods to encode the query, fragment, path parts etc. - but don't expose the "raw" encoding. This is unfortunate as fragment and query are allowed to encode space to +, so we don't want to use them. Path is encoded properly but is "normalized" first so we can't use it for 'generic' encoding either.

Best solution I could come up with:

return URLEncoder.encode(raw, StandardCharsets.UTF_8).replaceAll("\\+", "%20");

If replaceAll() is too slow for you, I guess the alternative is to roll your own encoder...

EDIT: I had this code in here first which doesn't encode "?", "&", "=" properly:

//don't use - doesn't properly encode "?", "&", "="
new URI(null, null, null, raw, null).toString().substring(1);
咋地 2024-10-29 01:35:22

编辑: URIUtil 在更新的版本中不再可用,更好的答案在 Java - 编码 URL 或由 Sindi 先生在此线程中编写。


URIUtil 确实很有用,尽管有一些 替代方案

URIUtil.encodeQuery(url);

例如,它将空格编码为“+”而不是“%20”

两者都是 在正确的上下文中完全有效。不过,如果您确实愿意,可以发出字符串替换。

EDIT: URIUtil is no longer available in more recent versions, better answer at Java - encode URL or by Mr. Sindi in this thread.


URIUtil of Apache httpclient is really useful, although there are some alternatives

URIUtil.encodeQuery(url);

For example, it encodes space as "+" instead of "%20"

Both are perfectly valid in the right context. Although if you really preferred you could issue a string replace.

冰葑 2024-10-29 01:35:22

没有必要在查询中将冒号编码为 %3B,尽管这样做并不违法。

URI         = scheme ":" hier-part [ "?" query ] [ "#" fragment ]
query       = *( pchar / "/" / "?" )
pchar         = unreserved / pct-encoded / sub-delims / ":" / "@"
unreserved    = ALPHA / DIGIT / "-" / "." / "_" / "~"
pct-encoded   = "%" HEXDIG HEXDIG
sub-delims    = "!" / "$" / "&" / "'" / "(" / ")" / "*" / "+" / "," / ";" / "="

似乎只有百分比编码的空格才有效,因为我怀疑空格是 ALPHA 或 DIGIT

外观 URI 规范了解更多详细信息。

It is not necessary to encode a colon as %3B in the query, although doing so is not illegal.

URI         = scheme ":" hier-part [ "?" query ] [ "#" fragment ]
query       = *( pchar / "/" / "?" )
pchar         = unreserved / pct-encoded / sub-delims / ":" / "@"
unreserved    = ALPHA / DIGIT / "-" / "." / "_" / "~"
pct-encoded   = "%" HEXDIG HEXDIG
sub-delims    = "!" / "
quot; / "&" / "'" / "(" / ")" / "*" / "+" / "," / ";" / "="

It also seems that only percent-encoded spaces are valid, as I doubt that space is an ALPHA or a DIGIT

look to the URI specification for more details.

Hello爱情风 2024-10-29 01:35:22

内置的 Java URLEncoder 正在做它应该做的事情,你应该使用它。

“+”或“%20”都是 URL 中空格字符的有效替换。任何一个都会起作用。

“:”应该被编码,因为它是一个分隔符。即 http://fooftp:// /栏。事实上,特定浏览器可以在未编码的情况下处理它,但这并不意味着它是正确的。你应该对它们进行编码。

作为良好实践,请务必使用采用字符编码参数的方法。那里通常使用 UTF-8,但您应该明确提供它。

URLEncoder.encode(yourUrl, "UTF-8");

The built in Java URLEncoder is doing what it's supposed to, and you should use it.

A "+" or "%20" are both valid replacements for a space character in a URL. Either one will work.

A ":" should be encoded, as it's a separator character. i.e. http://foo or ftp://bar. The fact that a particular browser can handle it when it's not encoded doesn't make it correct. You should encode them.

As a matter of good practice, be sure to use the method that takes a character encoding parameter. UTF-8 is generally used there, but you should supply it explicitly.

URLEncoder.encode(yourUrl, "UTF-8");
末蓝 2024-10-29 01:35:22

我只是想添加另一种方法来解决这个问题。

如果你的项目依赖于spring web,你可以使用他们的utils。

import org.springframework.web.util.UriUtils

import java.nio.charset.StandardCharsets

UriUtils.encode('vip:104534049:5', StandardCharsets.UTF_8)

输出:

vip%3A104534049%3A5

I just want to add anther way to resolve this problem.

If your project depends on spring web, you can use their utils.

import org.springframework.web.util.UriUtils

import java.nio.charset.StandardCharsets

UriUtils.encode('vip:104534049:5', StandardCharsets.UTF_8)

Output:

vip%3A104534049%3A5

愚人国度 2024-10-29 01:35:22
String param="2019-07-18 19:29:37";
param="%27"+param.trim().replace(" ", "%20")+"%27";

我观察到日期时间(时间戳)
URLEncoder.encode(param,"UTF-8") 不起作用。

String param="2019-07-18 19:29:37";
param="%27"+param.trim().replace(" ", "%20")+"%27";

I observed in case of Datetime (Timestamp)
URLEncoder.encode(param,"UTF-8") does not work.

故事和酒 2024-10-29 01:35:22

使用 URLEncoder.encode 时,空白字符“”会转换为 + 号。这与 JavaScript 等其他编程语言相反,JavaScript 将空格字符编码为 %20。但它是完全有效的,因为查询字符串参数中的空格由 + 表示,而不是 %20。 %20 通常用于表示 URI 本身中的空格(?之前的 URL 部分)。

The white space character " " is converted into a + sign when using URLEncoder.encode. This is opposite to other programming languages like JavaScript which encodes the space character into %20. But it is completely valid as the spaces in query string parameters are represented by +, and not %20. The %20 is generally used to represent spaces in URI itself (the URL part before ?).

我是有多爱你 2024-10-29 01:35:22

如果您的网址中只有空格问题。我使用了下面的代码,它工作正常

String url;
URL myUrl = new URL(url.replace(" ","%20"));

示例: url 是

www.xyz.com?para=你好先生

那么 muUrl 的输出是

www.xyz.com?para=hello%20sir

if you have only space problem in url. I have used below code and it work fine

String url;
URL myUrl = new URL(url.replace(" ","%20"));

example : url is

www.xyz.com?para=hello sir

then output of muUrl is

www.xyz.com?para=hello%20sir

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