我正在测试 PHP urlencode()
与 Java java.net.URLEncoder.encode()
.
Java
String all = "";
for (int i = 32; i < 256; ++i) {
all += (char) i;
}
System.out.println("All characters: -||" + all + "||-");
try {
System.out.println("Encoded characters: -||" + URLEncoder.encode(all, "utf8") + "||-");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
PHP
$all = "";
for($i = 32; $i < 256; ++$i)
{
$all = $all.chr($i);
}
echo($all.PHP_EOL);
echo(urlencode(utf8_encode($all)).PHP_EOL);
除了“星号”字符不是由 Java 编码并由 PHP 转换为 %2A 之外,这两个函数似乎都以相同的方式对所有字符进行编码。哪种行为应该是“正确的”行为(如果有)?
注意:我也尝试使用 rawurlencode()
- 没有运气。
最佳答案
可以在 URL 中包含 *
,(但也可以采用编码形式)。
RFC1738: Uniform Resource Locators (URL)陈述如下:
Reserved:
[...]
Usually a URL has the same interpretation when an octet is represented by a character and when it encoded. However, this is not true for reserved characters: encoding a character reserved for a particular scheme may change the semantics of a URL.
Thus, only alphanumerics, the special characters
"$-_.+!*'(),"
, and reserved characters used for their reserved purposes may be used unencoded within a URL.On the other hand, characters that are not required to be encoded (including alphanumerics) may be encoded within the scheme-specific part of a URL, as long as they are not being used for a reserved purpose.
关于java - urlencode() 'asterisk'(星号?)字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6533561/