我想用(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[]。
这个curl命令工作良好,但使用Http-组件,而我得到的参数完全相同。
也许我做错了什么。
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 );这篇文章的内容如下:
POST params : project=Test&string-keys%5B%5D=test.one&strings%5B%5D=TestOne&string-keys%5B%5D=test.two&strings%5B%5D=TestTwo如果我把它们放在后面--数据是卷曲的,它可以工作,但不适用于HttpCoponents。有人能解释一下为什么吗?
提前感谢
发布于 2011-09-05 14:53:25
尝试在您的httpRequest中添加标题“application/x form-urlencoded”。
httpRequest.addHeader("content-type", "application/x-www-form-urlencoded");
HttpResponse response = new DefaultHttpClient().execute( httpRequest );希望这能起作用
https://stackoverflow.com/questions/7309237
复制相似问题