post 方法中的 org.apache.commons.httpclient.NameValuePair
我正在编写一些代码,例如:
PostMethod p = new PostMethod(someurl);
...
NameValuePair[] data = {
new NameValuePair("name1", "somevalue1"),
new NameValuePair("var[3][1]", "10")
};
try {
hc.executeMethod(p);
}
...
当我在 Wireshark 中查看我的帖子时,这就是我得到的结果:
POST /someurl HTTP/1.1
...
type=var&ship%5B3%5D%5B1%5D=10
%5B
意味着 [
, %5D
- ]
所以问题是我如何在我的帖子中得到方括号?
I'm writing some code like :
PostMethod p = new PostMethod(someurl);
...
NameValuePair[] data = {
new NameValuePair("name1", "somevalue1"),
new NameValuePair("var[3][1]", "10")
};
try {
hc.executeMethod(p);
}
...
And that's what I get, when I look at my post in Wireshark:
POST /someurl HTTP/1.1
...
type=var&ship%5B3%5D%5B1%5D=10
%5B
means [
, %5D
- ]
So the problem is how I can get square brackets in my post?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这正是
POST
正文应该的样子。这些方括号必须经过 url 编码。当客户端解析 POST 正文中的查询字符串时,它应该对这些键和值进行 url 解码。在 Web 浏览器上使用简单的 HTML POST 表单进行尝试,并使用wireshark进行检查。你会看到完全相同的事情。这里没有问题。That's exactly what the
POST
body should look like. Those square braces must be url encoded. When the client parses the querystring in thePOST
body, it's supposed to url decode those keys and values. Try it with a simple HTML POST form on a web browser and check it with wireshark. You'll see exactly the same thing. There is no problem here.这称为编码,这就是数据传输的方式。但是,接收数据的服务将在其端对此进行解码。所以您无需担心这一点。
您无法在不编码的情况下按原样发送方括号,因为它们被认为是不安全的。以下是来自 URL RFC 的文本
This is called encoding and this is how data is transferred. However, the service receiving the data will decode this on its end. So you no need to worry about this.
You can't send the square brackets as-is without encoding as they are considered unsafe. Below is the text from URL RFC