开发者

org.apache.commons.httpclient.NameValuePair in post method

开发者 https://www.devze.com 2023-01-31 22:33 出处:网络
I\'m writing some code like : PostMethod p = new PostMethod(someurl); ... NameValuePair[] data = { new NameValuePair(\"name1\", \"somevalue1\"),

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&sh开发者_运维技巧ip%5B3%5D%5B1%5D=10

%5B means [, %5D- ]

So the problem is how I can get square brackets in my post?


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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消