java - 谁对 URLencoding 进行解码?

标签 java decoding url-encoding

我已经使用 URLEncoder 类对我的字符串(例如 String a="123+gtyt")进行了编码。编码后的字符串是 String b。然后我发送“String b”作为附加到 URL 的参数。让我们说http://example.com?request=b

当我使用 URLDecoder 解码 example.com 上的字符串时,字符串中的符号 + 丢失,解码后我没有得到“String a”

现在,当我在 example.com 上打印而不解码“String b”时。我准确地得到了 String a



所以我怀疑重定向时解码是否是由浏览器本身完成的?

最佳答案

当您对“123+gtyt”进行编码时,它会对加号进行编码。

当您处理 HTTP 请求时,servlet API 会自动将其解码为“123+gtyt”。如果您再次解码它 - 它将把“+”更改为空格。

所以关键是 - 不要显式解码参数。

例如:

    final String encoded = URLEncoder.encode("123+gtyt");
    final String decoded = URLDecoder.decode(encoded);
    System.out.println("decoded = " + decoded); // 123+gtyt
    System.out.println("URLDecoder.decode(decoded) = " 
              + URLDecoder.decode(decoded)); // prints 123 gtyt

关于java - 谁对 URLencoding 进行解码?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10153874/

相关文章:

java - 使用 Netty 将 websockets 与在 tomcat 中运行的 Spring Web 应用程序集成

java - 使用 jxl for java 指定 WritableHyperlink 的单元格格式

ffmpeg - 在处理 MPEG4V1 数据方面需要帮助

android - 在 Android 上使用 OpenMAX (IL?) 进行音频/视频解码

java - 如何避免从 URL.getFile() 获取 URL 编码路径?

jquery - 使用jquery ajax将本地url连接到绝对url

java - 从数组列表和链表的末尾一一删除 100 万个整数

java - 如何解析可选 URL 参数

c# - aspnet_Membership 表中有解密密码的方法吗?

string - 设置 POST 请求表单数据变量