Java 使用 HTTP Get 发送数组

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3061273/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 16:07:05  来源:igfitidea点击:

Send an Array with an HTTP Get

javahttpgwtarrays

提问by Matteo

How can i send an Array with a HTTP Get request?

如何使用 HTTP Get 请求发送数组?

I'm Using GWT client to send the request.

我正在使用 GWT 客户端发送请求。

采纳答案by BalusC

That depends on what the target server accepts. There is no definitive standard for this. See also a.o. Wikipedia: Query string:

这取决于目标服务器接受什么。对此没有明确的标准。另见 ao维基百科:查询字符串

While there is no definitive standard, most web frameworks allow multiple values to be associated with a single field (e.g. field1=value1&field1=value2&field2=value3).[4][5]

虽然没有明确的标准,但大多数 Web 框架允许将多个值与单个字段相关联(例如field1=value1&field1=value2&field2=value3)。[4] [5]

Generally, when the target server uses a strong typedprogramming language like Java (Servlet), then you can just send them as multiple parameters with the same name. The API usually offers a dedicated method to obtain multiple parameter values as an array.

通常,当目标服务器使用Java ( Servlet)等强类型编程语言时,您可以将它们作为多个具有相同名称的参数发送。API 通常提供专门的方法来获取多个参数值作为数组。

foo=value1&foo=value2&foo=value3
String[] foo = request.getParameterValues("foo"); // [value1, value2, value3]

The request.getParameter("foo")will also work on it, but it'll return only the first value.

request.getParameter("foo")也将在它的工作,但它会只返回第一个值。

String foo = request.getParameter("foo"); // value1

And, when the target server uses a weak typedlanguage like PHP or RoR, then you need to suffix the parameter name with braces []in order to trigger the language to return an array of values instead of a single value.

而且,当目标服务器使用弱类型语言(如 PHP 或 RoR)时,您需要在参数名称后加上大括号[],以触发该语言返回值数组而不是单个值。

foo[]=value1&foo[]=value2&foo[]=value3
$foo = $_GET["foo"]; // [value1, value2, value3]
echo is_array($foo); // true

In case you still use foo=value1&foo=value2&foo=value3, then it'll return only the first value.

如果您仍然使用foo=value1&foo=value2&foo=value3,那么它只会返回第一个值。

$foo = $_GET["foo"]; // value1
echo is_array($foo); // false

Do note that when you send foo[]=value1&foo[]=value2&foo[]=value3to a Java Servlet, then you can still obtain them, but you'd need to use the exact parameter name including the braces.

请注意,当您发送foo[]=value1&foo[]=value2&foo[]=value3到 Java Servlet 时,您仍然可以获取它们,但是您需要使用包括括号在内的确切参数名称。

String[] foo = request.getParameterValues("foo[]"); // [value1, value2, value3]

回答by David Roman

I know this post is really old, but I have to reply because although BalusC's answer is marked as correct, it's not completely correct.

我知道这个帖子真的很旧,但我必须回复,因为虽然 BalusC 的答案被标记为正确,但并不完全正确。

You have to write the query adding "[]" to foo like this:

您必须编写将“[]”添加到 foo 的查询,如下所示:

foo[]=val1&foo[]=val2&foo[]=val3