java - urlencode() 'asterisk'(星号?)字符

标签 java php urlencode

我正在测试 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/

相关文章:

django - urlencode 过滤器在 Django 模板中不起作用

java - 创建动态增长的数组所需的工作单元

java - Java中如何限制最大线程数?

javascript - 无法解决Ajax、PHP、Json、Laravel(PHP框架)中的代码

c# - 为什么 System.Web.HttpUtility.UrlEncode 给出的命名空间名称在 Visual C# 2008 中不存在?

c# - UrlPathEncode 与 UrlEncode

java - 使用 Eclipse 在 Apache Tomcat 中出现错误 404

java - 为 log4j 添加微秒级

php - 如何将数据从 MYSQL/PHP 发送到 Coldfusion 组件?

PHP静态函数self::in joomla JFactory类解释?