如何使用HttpComponents发布数组参数
我想用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需要字符串和字符串键参数作为数组 ,这意味着为每个参数重复字符串[]和字符串键[] 。
这个curl命令工作正常,但是使用Http组件,而我得到了完全相同的参数。
也许我做错了什么。
List params = new ArrayList(); 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 );
POST参数看起来像:
POST params : project=Test&string-keys%5B%5D=test.one&strings%5B%5D=TestOne&string-keys%5B%5D=test.two&strings%5B%5D=TestTwo
如果我将它们放在curl中的–data后面,它可以工作,但不适用于HttpCoponents。 有人能解释我为什么吗?
提前致谢
尝试在httpRequest中添加标题“application / x-www-form-urlencoded”
httpRequest.addHeader("content-type", "application/x-www-form-urlencoded"); HttpResponse response = new DefaultHttpClient().execute( httpRequest );
希望这会奏效
- 如何使用RestTemplate为每个请求设置RequestConfiguration?
- Apache HttpClient GET与body
- 尝试进行客户端服务器ssl身份validation时,Tomcat 7获取SSLv2Hello被禁用错误
- java.net.SocketException:打开的文件太多
- 如何从apache httpclient 4中禁用默认请求标头?
- HttpClient 4.3.x,修复已弃用的代码以使用当前的HttpClient实现
- 如何使Unirest(java)忽略证书错误
- SSLHandshakeException:证书中的主机名不匹配
- 什么是apache中的SchemeRegistry以及何时应该使用它?