必要时用于URL编码的Java库(如浏览器)

pal*_*int 10 java urlencode

如果我将http://localhost:9000/space testURL 放到Web浏览器的地址栏中,它会调用服务器http://localhost:9000/space%20test. http://localhost:9000/specÁÉÍtest也将被编码为http://localhost:9000/spec%C3%81%C3%89%C3%8Dtest.

如果将编码的URL放到地址栏(即http://localhost:9000/space%20test和http://localhost:9000/spec%C3%81%C3%89%C3%8Dtest),它们保持不变(它们不会被双重编码).

是否有任何Java API或库执行此编码?URL来自用户,因此我不知道它们是否已编码.

(如果没有,那么%在输入字符串中搜索是否足够并且如果找不到则编码,或者是否有任何特殊情况这不起作用?)

编辑:

URLEncoder.encode("space%20test", "UTF-8")返回space%2520test不是我想要的,因为它是双重编码的.

编辑2:

此外,浏览器处理部分编码的URL,例如http://localhost:9000/specÁÉ%C3%8Dtest,没有对它们进行双重编码.在这种情况下,服务器接收到以下网址:http://localhost:9000/spec%C3%81%C3%89%C3%8Dtest.它与编码形式相同...specÁÉÍtest.

Ven*_*min 10

每个Web开发人员必须了解的URL编码

网址编码解释

为什么我需要URL编码?

The URL specification RFC 1738 specifies that only a small set of characters 
can be used in a URL. Those characters are:

A to Z (ABCDEFGHIJKLMNOPQRSTUVWXYZ)
a to z (abcdefghijklmnopqrstuvwxyz)
0 to 9 (0123456789)
$ (Dollar Sign)
- (Hyphen / Dash)
_ (Underscore)
. (Period)
+ (Plus sign)
! (Exclamation / Bang)
* (Asterisk / Star)
' (Single Quote)
( (Open Bracket)
) (Closing Bracket)
Run Code Online (Sandbox Code Playgroud)

URL编码如何工作?

All offending characters are replaced by a % and a two digit hexadecimal value 
that represents the character in the proper ISO character set. Here are a 
couple of examples:

$ (Dollar Sign) becomes %24
& (Ampersand) becomes %26
+ (Plus) becomes %2B
, (Comma) becomes %2C
: (Colon) becomes %3A
; (Semi-Colon) becomes %3B
= (Equals) becomes %3D
? (Question Mark) becomes %3F
@ (Commercial A / At) becomes %40
Run Code Online (Sandbox Code Playgroud)

简单示例:

import java.util.logging.Level;
import java.util.logging.Logger;
import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import javax.script.ScriptException;

public class TextHelper {
    private static ScriptEngine engine = new ScriptEngineManager()
        .getEngineByName("JavaScript");

/**
 * Encoding if need escaping %$&+,/:;=?@<>#%
 *
 * @param str should be encoded
 * @return encoded Result 
 */
public static String escapeJavascript(String str) {
    try {
        return engine.eval(String.format("escape(\"%s\")", 
            str.replaceAll("%20", " "))).toString()
                .replaceAll("%3A", ":")
                .replaceAll("%2F", "/")
                .replaceAll("%3B", ";")
                .replaceAll("%40", "@")
                .replaceAll("%3C", "<")
                .replaceAll("%3E", ">")
                .replaceAll("%3D", "=")
                .replaceAll("%26", "&")
                .replaceAll("%25", "%")
                .replaceAll("%24", "$")
                .replaceAll("%23", "#")
                .replaceAll("%2B", "+")
                .replaceAll("%2C", ",")
                .replaceAll("%3F", "?");
    } catch (ScriptException ex) {
        Logger.getLogger(TextHelper.class.getName())
            .log(Level.SEVERE, null, ex);
        return null;
    }
}
Run Code Online (Sandbox Code Playgroud)


Veg*_*ger 8

使用java java.net.URLEncoder#encode():

String page = "space test";
String ecodedURL = "http://localhost:9000/" + URLEncoder.encode(page, "UTF-8");
Run Code Online (Sandbox Code Playgroud)

注意:对完整的URL进行编码会导致意外情况,例如http://编码http%3A%2F%2F!

编辑:为防止对URL进行两次编码,您可以检查URL是否包含a,%因为它仅对编码有效.但是如果一个用户错误地搞砸了编码(比如,只是部分地编码URL或者%在URL中使用它而不用它来编码某些东西)那么使用这种方法没什么可做的......