简体   繁体   English

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

[英]Emulating HTTP POST via httpclient 3.x for multi options

I want to emulate a HTTP POST using application/x-www-form-urlencoded encoding to send a option group that allows multiple selections. 我想使用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>

Does adding 2 NameValuePairs (NVP) with the same name work ? 添加两个具有相同名称的NameValuePairs(NVP)是否有效? My serverside log shows that only the first NVP was received. 我的服务器端日志显示仅收到第一个NVP。

eg 例如

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

Only the groups=1 parameter was received. 仅收到groups = 1参数。 Thanks 谢谢

More likely is that your server code is calling ServletRequest.getParameter() rather than getParameterValues() . 您的服务器代码更有可能正在调用ServletRequest.getParameter()而不是getParameterValues()

But the best way to verify is use an HTTP proxy such as Fiddler to look at the actual request. 但最好的验证方法是使用HTTP代理(例如Fiddler)查看实际请求。


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. 编辑:正确的HttpClient方法是addParameters() ,而不是addParameter() -您的代码显示了后者,但是我不相信它将编译,因此假设您复制不正确。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM