Qiao_Y
Qiao_Y
2016-06-12 03:29

httpclient的post调用sptingmvc服务,传递json到后台

httpclient的post调用springmvc服务,传递json到后台,在后台怎么接受这个json串,是想只接受这个json字符串,不是json对象

这是调用过程:

            String response = null;
            String params = "{\"userInfo\":\"18575584371\"}";
            int timeoutConnection = 3000;
            int timeoutSocket = 5000;

            HttpParams httpParameters = new BasicHttpParams();

            HttpConnectionParams.setConnectionTimeout(httpParameters, timeoutConnection);
            HttpConnectionParams.setSoTimeout(httpParameters, timeoutSocket);

            HttpClient httpClient = new DefaultHttpClient(httpParameters);

            HttpPost httpPost = new HttpPost(targetURL);
            System.out.println(params);
            StringEntity entity = new StringEntity(params,"utf-8");//解决中文乱码问题    
            entity.setContentEncoding("UTF-8");    
            entity.setContentType("application/json");
            httpPost.setEntity(entity);
            HttpResponse httpResponse;
            httpResponse = httpClient.execute(httpPost);

            int statusCode = httpResponse.getStatusLine().getStatusCode();
            if (statusCode == HttpStatus.SC_OK) {
                // 获得返回结果
                response = EntityUtils.toString(httpResponse.getEntity());
            } else {
                response = "返回码:" + statusCode;
            }
            System.out.println(response);

后台接收:

 @RequestMapping(value = "/user/login", method = RequestMethod.POST)
    public Model login(Model model, @RequestParam String userInfo, HttpServletRequest request) {……}

怎么都接收不到,400错误,求大神帮忙看看

  • 点赞
  • 回答
  • 收藏
  • 复制链接分享

3条回答

为你推荐

换一换