【问题标题】:How to pass Object like parameters with HttpPost如何使用 HttpPost 传递类似对象的参数
【发布时间】:2014-11-05 12:08:33
【问题描述】:

我正在通过这种方法使用 RESTfull 网络服务:

@RequestMapping(value = "/rest/secure/Userprofile/{providerId}", method = RequestMethod.POST)
@ResponseBody 
public List<Userprofile> addUserprofile(@RequestBody Object[] socialAccounts, @PathVariable String providerId, HttpServletRequest request, HttpServletResponse response) {
       System.out.println("do something!!!");
}

我想传递 socialAccount 对象。

    String json = "{\"id\":\"26651480000\",\"selected\":true,\"category\":\"Software\",\"perms\":[\"ADMINISTER\",\"EDIT_PROFILE\",\"CREATE_CONTENT\",\"MODERATE_CONTENT\",\"CREATE_ADS\",\"BASIC_ADMIN\"],\"provideruserid\":\"1475334273\",\"name\":\"Eran\",\"useraccesstoken\":\"CAABletmsJHgBANXAhAlaQXVN1TrI5Tq8gvU002Ke8ZB2dcAhoo21u8orrHiT77G3cm6CmJ4zBX2mu8koeq\",\"checked\":false,\"access_token\":\"CAAB0000wcuq8O\"}";

    CloseableHttpClient httpclient = HttpClients.createDefault();
    URIBuilder builder = new URIBuilder();
    builder.setScheme("http").setHost("localhost:8080/AppDev")
    .setPath("/rest/secure/Userprofile/facebook/");
    URI uri = builder.build();

    HttpPost httppost = new HttpPost(uri);
    ArrayList<NameValuePair> postParameters;
    postParameters = new ArrayList<NameValuePair>();
    postParameters.add(new BasicNameValuePair("socialAccounts", json));
    httppost.setEntity(new UrlEncodedFormEntity(postParameters));       
    httppost.setHeader("X-Auth-Token", userLogged.getToken());
    httppost.addHeader("Content-Type", "application/json;charset=UTF-8");
    CloseableHttpResponse response = httpclient.execute(httppost);

我明白了:HTTP/1.1 400 错误请求

一些建议?

【问题讨论】:

  • 您应该指定一个Content-Type 标头,以便端点知道如何解码请求的内容(不过我认为这不是答案,只是提示)

标签: parameters http-post apache-httpcomponents


【解决方案1】:

我无法发表评论,所以我必须将其作为答案。 也不确定,如果这能解决你的问题 但实际上,您应该替换调用:

CloseableHttpResponse response = httpclient.execute(httpget);

CloseableHttpResponse response = httpclient.execute(httppost);

【讨论】:

    【解决方案2】:

    行得通。

    CloseableHttpClient httpclient = HttpClients.createDefault();
            URIBuilder builder = new URIBuilder();
            builder.setScheme("http").setHost("localhost:8080/AppDev")
            .setPath("/rest/secure/Userprofile/facebook/");
            URI uri;
            try {
                uri = builder.build();
    
                HttpPost http = new HttpPost(uri);
                http.setHeader("X-Auth-Token", userLogged.getToken());
                http.addHeader("content-type", "application/json");
    
                String json = "[{\"category\":\"Public figure\",\"category_list\":[{\"id\":\"1756452480085\",\"name\":\"Catholic Church\"}],\"name\":\"Pope Francesco fans\",\"access_token\":\"token1\",\"perms\":[\"ADMINISTER\",\"EDIT_PROFILE\",\"CREATE_CONTENT\",\"MODERATE_CONTENT\",\"CREATE_ADS\",\"BASIC_ADMIN\"],\"id\":\"346277202148996\",\"checked\":false,\"selected\":true,\"provideruserid\":\"1475334273\",\"useraccesstoken\":\"usertoken\"},{\"category\":\"Shopping/retail\",\"category_list\":[{\"id\":\"2006019953504\",\"name\":\"Shopping & Retail\"}],\"name\":\"fanpage\",\"access_token\":\"token2\",\"perms\":[\"ADMINISTER\",\"EDIT_PROFILE\",\"CREATE_CONTENT\",\"MODERATE_CONTENT\",\"CREATE_ADS\",\"BASIC_ADMIN\"],\"id\":\"535193443194952\",\"checked\":false,\"selected\":true,\"provideruserid\":\"1475334273\",\"useraccesstoken\":\"usertoken2\"}]";
    
                StringEntity params = new StringEntity(json);
                http.setEntity(params);
                CloseableHttpResponse response = httpclient.execute(http);
    
                try {
                    HttpEntity entity = response.getEntity();
                    if (entity != null) {
                        String json1 = EntityUtils.toString(entity);
    
                        System.out.println(json1);
                    }
                } finally {
                    response.close();
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-07-10
      • 1970-01-01
      • 2012-03-30
      • 1970-01-01
      相关资源
      最近更新 更多