问题描述
我想用 Apache http-components (4.1.2) 执行这个命令
curl --data "strings[]=testOne&string-keys[]=test.one&strings[]=testTwo&string-keys[]=test.two&project=Test" https:///api.foo.com/1/string/input-bulk
目标api需要strings和string-keys参数作为array,这意味着重复strings[]和 string-keys[] 用于每个参数.
这个 curl 命令工作正常,但使用 Http 组件,而我得到的参数完全相同.
也许我做错了什么.
Listparams = new ArrayList();params.add( new BasicNameValuePair( "project", PROJECT_NAME));对于(条目条目:newEntries){params.add( new BasicNameValuePair( "string-keys[]", entry.getKey()));params.add( new BasicNameValuePair( "strings[]", entry.getValue()));params.add( new BasicNameValuePair( "context[]", "" ));}URI uri = URIUtils.createURI( "https", "api.foo.com", -1, "/1/string/input-bulk", null, null );UrlEncodedFormEntity paramEntity = new UrlEncodedFormEntity( params);logger.info("POST 参数:{}",EntityUtils.toString(paramEntity));HttpPost httpRequest = new HttpPost( uri );httpRequest.setEntity( paramEntity );HttpResponse 响应 = 新的 DefaultHttpClient().execute(httpRequest);
POST 参数如下所示:
POST 参数:project=Test&string-keys%5B%5D=test.one&strings%5B%5D=TestOne&string-keys%5B%5D=test.two&strings%5B%5D=测试二
如果我将它们放在 curl 中的 --data 后面,它可以工作,但不适用于 HttpCoponents.有人能解释一下为什么吗?
提前致谢
尝试在您的 httpRequest 添加标题application/x-www-form-urlencoded"
httpRequest.addHeader("content-type", "application/x-www-form-urlencoded");HttpResponse 响应 = 新的 DefaultHttpClient().execute(httpRequest);希望能奏效
I want to perform this command with Apache http-components (4.1.2)
curl --data "strings[]=testOne&string-keys[]=test.one&strings[]=testTwo&string-keys[]=test.two&project=Test" https://api.foo.com/1/string/input-bulk
The target api need strings and string-keys parameters as array, which mean repeating strings[] and string-keys[] for each parameter.
This curl command works fine but with Http-component, while I got exactly the same parameters.
Maybe I'm doing something wrong.
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add( new BasicNameValuePair( "project", PROJECT_NAME ) );
for ( Entry entry : newEntries )
{
params.add( new BasicNameValuePair( "string-keys[]", entry.getKey() ) );
params.add( new BasicNameValuePair( "strings[]", entry.getValue() ) );
params.add( new BasicNameValuePair( "context[]", "" ) );
}
URI uri = URIUtils.createURI( "https", "api.foo.com", -1, "/1/string/input-bulk", null, null );
UrlEncodedFormEntity paramEntity = new UrlEncodedFormEntity( params );
logger.info( "POST params : {}", EntityUtils.toString( paramEntity ) );
HttpPost httpRequest = new HttpPost( uri );
httpRequest.setEntity( paramEntity );
HttpResponse response = new DefaultHttpClient().execute( httpRequest );
The POST params looks like :
POST params : project=Test&string-keys%5B%5D=test.one&strings%5B%5D=TestOne&string-keys%5B%5D=test.two&strings%5B%5D=TestTwo
If I put them behind --data in curl, it works, but not with HttpCoponents.Can someone explain me why?
Thanks in advance
Try adding the header "application/x-www-form-urlencoded" in your httpRequest
httpRequest.addHeader("content-type", "application/x-www-form-urlencoded");
HttpResponse response = new DefaultHttpClient().execute( httpRequest );
Hopefully that will work
这篇关于如何使用 HttpComponents 发布数组参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!