通过 httpclient 3.x 模拟 HTTP POST 以获得多个选项

发布于 2024-08-30 19:19:58 字数 582 浏览 2 评论 0原文

我想使用 application/x-www-form-urlencoded 编码模拟 HTTP POST 发送允许多项选择的选项组。

<select name="groups" multiple="multiple" size="4">
    <option value="2">Administration</option>
    <option value="1">General</option>
</select>

添加 2 个同名的 NameValuePairs (NVP) 是否有效?我的服务器端日志显示仅收到第一个 NVP。

例如,

PostMethod method = ...;
NameValuePair[] nvpairs = {
    new NameValuePair( "groups", "2" );
    new NameValuePair( "groups", "1" );
};
method.addParameter( nvpairs );

仅收到 groups=1 参数。谢谢

I want to emulate a HTTP POST using application/x-www-form-urlencoded encoding
to send a option group that allows multiple selections.

<select name="groups" multiple="multiple" size="4">
    <option value="2">Administration</option>
    <option value="1">General</option>
</select>

Does adding 2 NameValuePairs (NVP) with the same name work ? My serverside log shows that only the first NVP was received.

e.g

PostMethod method = ...;
NameValuePair[] nvpairs = {
    new NameValuePair( "groups", "2" );
    new NameValuePair( "groups", "1" );
};
method.addParameter( nvpairs );

Only the groups=1 parameter was received. Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

静赏你的温柔 2024-09-06 19:19:58

更有可能的是,您的服务器代码正在调用 ServletRequest.getParameter() 而不是 getParameterValues()

但最好的验证方法是使用 HTTP 代理(例如 Fiddler)来查看实际请求。


编辑:正确的 HttpClient 方法是 addParameters(),而不是 addParameter() - 您的代码显示后者,但我不相信它会编译,所以假设您复制不正确。

More likely is that your server code is calling ServletRequest.getParameter() rather than getParameterValues().

But the best way to verify is use an HTTP proxy such as Fiddler to look at the actual request.


Edit: the correct HttpClient method is addParameters(), not addParameter() -- your code shows the latter, but I don't believe it would compile so am assuming you copied incorrectly.

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