如何在 Java 中对 url 上的查询参数进行编码?我知道,这似乎是一个显而易见且已经被问过的问题。
有两个微妙之处我不确定:
- url 上的空格应该编码为“+”还是“%20”?在 chrome 中,如果我输入“http://google.com/foo=?bar me”,chrome 会将其更改为使用 %20 进行编码,
- 是否有必要/正确地将冒号“:”编码为 %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:
- 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
- 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
发布评论
评论(9)
java.net.URLEncoder.encode(String s, String编码)
也可以提供帮助。它遵循 HTML 表单编码application/x-www-form-urlencoded
。另一方面,百分比编码(也称为URL 编码) 使用
%20
对空格进行编码。冒号是保留字符,因此编码后:
仍将保留冒号。java.net.URLEncoder.encode(String s, String encoding)
can help too. It follows the HTML form encodingapplication/x-www-form-urlencoded
.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.不幸的是, URLEncoder.encode() 不会产生有效的百分比编码(如 中指定的) RFC 3986)。
URLEncoder.encode() 可以很好地对所有内容进行编码,除了空格被编码为“+”。我能找到的所有 Java URI 编码器都只公开公共方法来编码查询、片段、路径部分等,但不公开“原始”编码。不幸的是,因为片段和查询允许将空格编码为 +,所以我们不想使用它们。路径已正确编码,但首先“标准化”,因此我们也不能将其用于“通用”编码。
我能想到的最佳解决方案:
如果
replaceAll()
对你来说太慢,我想替代方案是滚动你自己的编码器...编辑:我首先在这里有这个代码,它不会t 正确编码“?”、“&”、“=”:
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:
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:
编辑:
URIUtil
在更新的版本中不再可用,更好的答案在 Java - 编码 URL 或由 Sindi 先生在此线程中编写。URIUtil 确实很有用,尽管有一些 替代方案
两者都是 在正确的上下文中完全有效。不过,如果您确实愿意,可以发出字符串替换。
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 alternativesBoth are perfectly valid in the right context. Although if you really preferred you could issue a string replace.
没有必要在查询中将冒号编码为 %3B,尽管这样做并不违法。
似乎只有百分比编码的空格才有效,因为我怀疑空格是 ALPHA 或 DIGIT
外观 URI 规范了解更多详细信息。
It is not necessary to encode a colon as %3B in the query, although doing so is not illegal.
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.
内置的 Java URLEncoder 正在做它应该做的事情,你应该使用它。
“+”或“%20”都是 URL 中空格字符的有效替换。任何一个都会起作用。
“:”应该被编码,因为它是一个分隔符。即 http://foo 或 ftp:// /栏。事实上,特定浏览器可以在未编码的情况下处理它,但这并不意味着它是正确的。你应该对它们进行编码。
作为良好实践,请务必使用采用字符编码参数的方法。那里通常使用 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.
我只是想添加另一种方法来解决这个问题。
如果你的项目依赖于spring web,你可以使用他们的utils。
输出:
I just want to add anther way to resolve this problem.
If your project depends on spring web, you can use their utils.
Output:
我观察到日期时间(时间戳)
URLEncoder.encode(param,"UTF-8")
不起作用。I observed in case of Datetime (Timestamp)
URLEncoder.encode(param,"UTF-8")
does not work.使用
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 ?).如果您的网址中只有空格问题。我使用了下面的代码,它工作正常
示例: url 是
那么 muUrl 的输出是
if you have only space problem in url. I have used below code and it work fine
example : url is
then output of muUrl is