1

例如,如果帖子网址是:

http://www.wolf.com/pcap/search?stime= {stime}&etime=${etime}&bpf=${bpf}

那么我们可以这样做:

Map<String, String> vars = new HashMap<String, String>();
vars.put("bpf", bpf);

...
responseString = restTemplate.postForObject(url, null, String.class,vars);

如果 bpf 是 String,那么 bpf 的大小是否有限制?可以是任意大小吗?

4

1 回答 1

1

Unfortunately the answer is: "It depends".

More precisely: as you append the bpf as a parameter to the URL it does not really matter if you are doing a POST or a GET. Sometimes there are restrictions on the length of an URL a server will handle, but that depends on what the server accepts, and cannot be determined from the RestTemplate, which is the client.

For example if the server you send the REST request to is a tomcat, then the maximal value of the complete header (URL, HTTP-Header etc) is by default 8kB for tomcat 6.0 or higher; see e.g. https://serverfault.com/questions/56691/whats-the-maximum-url-length-in-tomcat

Just in case if you have control over the server side, too, you can change the expected interface by not sending the bpf as parameter, but as request body, like:

Map<String, String> vars = new HashMap<String, String>();
// vars.put("bpf", bpf); <--- not needed
responseString = restTemplate.postForObject(url, bpf, String.class, vars);

(and then of course get the bpf on the server from the request body instead).

Otherwise you are out of luck and have to limit the length of the URL. Maybe use a proxy or network sniffer to see what extra Headers are actually send and subtract that from the 8kB limit to get the maximal length of the URL.

于 2013-02-10T16:05:22.770 回答