返回信息流当指明"Content-Type"为"application/x-www-form-urlencoded"的时候,表单数据就被装载到了请求体,那为什么装载到请求体的数据还需要以URL Encode方式编码呢?比如下面这段代码:
```java
for (Map.Entry<String, String> entry : postData.entrySet()) {
if (first) first = false;
else body.append("&");
// 这里直接用entry.getKey()不行吗?为啥要用URLEncoder.encode方法转换一次呢
body.append(URLEncoder.encode(entry.getKey(), "UTF-8"));
body.append("=");
body.append(URLEncoder.encode(entry.getValue(), "UTF-8"));
}
```
---
Google了一个解释,但感觉其实本质上确实是可以的
> Payloads using the text/plain format are intended to be human readable. They are not reliably interpretable by computer, as the format is ambiguous (for example, there is no way to distinguish a literal newline in a value from the newline at the end of the value).
这是一条镜像帖。来源:北邮人论坛 / www-technology / #41420同步于 2019/11/7
该镜像源已超过 30 天没有更新,可能在源站已被删除。
WWWTechnology机器人发帖
【已解决】【问题】为什么通过POST方式提交表单的时候需要URL E
youdianer
2019/11/7镜像同步15 回复
订阅后,新回复会通过你的通知中心匿名送达。
9 条回复
我就是想知道为啥要这样规定啊,直接放string不更方便吗?比如HTML5就引入了新的content-type:text/plain,那直接一开始就这样用不好吗?
【 在 autulin 的大作中提到: 】
: header的content type就是你告诉别人你用的url encode,所以body用什么编码你要跟你声称的一致才能让别人正确的解析啊
如果string有特殊字符就不好解析了,比如entry.getKey()是abc&def=ttt。 通过URLEncode.encode可以将其转换为abc%26def%20ttt
但是请求body里面放特殊字符有什么不好解析的啊?比如这里直接用abc&def=ttt也不会造成什么问题吧
【 在 yuluo114 的大作中提到: 】
: 如果string有特殊字符就不好解析了,比如entry.getKey()是abc&def=ttt。 通过URLEncode.encode可以将其转换为abc%26def%20ttt
空格和制表符都是有ascii编码的
【 在 luluxiu 的大作中提到: 】
: 哈哈哈,请问 aaa(空格)(Tab)bbb 你怎么传? “aaa bbb"吗? 哈哈哈
这样有什么问题吗?字符串里的空格不都是使用某种编码方式对空格进行编码的吗?就像你传递正常的"Hello World"一样啊。HTML5引入的content-type:text/plain不就是这样?
【 在 luluxiu 的大作中提到: 】
: 所以呢? 空格你传32 ? 0010 0000 ?