post 方法中的 org.apache.commons.httpclient.NameValuePair

发布于 2024-10-08 19:26:30 字数 480 浏览 0 评论 0原文

我正在编写一些代码,例如:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

夜空下最亮的亮点 2024-10-15 19:26:30

这正是 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 the POST 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.

掌心的温暖 2024-10-15 19:26:30

这称为编码,这就是数据传输的方式。但是,接收数据的服务将在其端对此进行解码。所以您无需担心这一点。

您无法在不编码的情况下按原样发送方括号,因为它们被认为是不安全的。以下是来自 URL RFC 的文本

不安全:

角色对于一个人来说可能是不安全的
原因的数量。空间
角色不安全,因为
重要的空格可能会消失并且
可能会引入无关紧要的空格
当 URL 被转录或
排版或经过处理
文字处理程序。这
字符“<”和“>”不安全
因为它们被用作
自由文本中 URL 周围的分隔符;
引号 (""") 用于
在某些系统中分隔 URL。这
字符“#”不安全,应该
总是被编码,因为它被使用
在万维网和其他领域
系统将 URL 与
片段/锚标识符可能
跟随它。字符“%”是
不安全,因为它用于
其他字符的编码。其他
字符不安全,因为
网关和其他运输代理
已知有时会修改此类
人物。这些字符是“{”,
“}”、“|”、“\”、“^”、“~”、“[”、“]”、
和“`”。

所有不安全字符必须始终
被编码在 URL 中。对于
例如,字符“#”必须是
即使在系统中也可以在 URL 中进行编码
通常不处理的
片段或锚标识符,所以
如果该 URL 被复制到另一个
确实使用它们的系统,它将
无需更改 URL
编码。

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

Unsafe:

Characters can be unsafe for a
number of reasons. The space
character is unsafe because
significant spaces may disappear and
insignificant spaces may be introduced
when URLs are transcribed or
typeset or subjected to the treatment
of word-processing programs. The
characters "<" and ">" are unsafe
because they are used as the
delimiters around URLs in free text;
the quote mark (""") is used to
delimit URLs in some systems. The
character "#" is unsafe and should
always be encoded because it is used
in World Wide Web and in other
systems to delimit a URL from a
fragment/anchor identifier that might
follow it. The character "%" is
unsafe because it is used for
encodings of other characters. Other
characters are unsafe because
gateways and other transport agents
are known to sometimes modify such
characters. These characters are "{",
"}", "|", "\", "^", "~", "[", "]",
and "`".

All unsafe characters must always
be encoded within a URL. For
example, the character "#" must be
encoded within URLs even in systems
that do not normally deal with
fragment or anchor identifiers, so
that if the URL is copied into another
system that does use them, it will
not be necessary to change the URL
encoding.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文