有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java组织。阿帕奇。平民httpclient。post方法中的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-]

所以问题是我如何在我的帖子中得到方括号


共 (2) 个答案

  1. # 1 楼答案

    这叫做编码,这就是数据传输的方式。然而,接收数据的服务将在其端对其进行解码。所以你不用担心这个

    由于方括号被视为不安全,如果不进行编码,则无法按原样发送方括号。下面是来自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.

  2. # 2 楼答案

    这正是POST体的外观。这些方括号必须是url编码的。当客户端解析POST主体中的查询字符串时,它应该对这些键和值进行url解码。在网络浏览器上用一个简单的HTML POST表单试试,然后用wireshark检查一下。你会看到完全相同的事情。这里没有问题