如何使用 HttpComponents 发布数组参数 [英] How to post array parameters with HttpComponents

查看:25
本文介绍了如何使用 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需要stringsstring-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 发布数组参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆