/** * 获取httpBody中的请求数据处理后并以JSON格式返回到httpBody中 测试@ReponseBody(有) @RequestBody(有) * * @param userLoginMap * @param request * @return * @throws IOException */ @RequestMapping(value = "/user/login1.json", method = RequestMethod.POST, headers = { "content-type=application/json" }) public @ResponseBody String login(@RequestBody Map<String, String> userLoginMap, HttpServletRequest request) throws IOException { if (userLoginMap.get("account") == null || userLoginMap.get("password") == null) { return "{\"code\":-400,\"message\":\"参数错误。\"}"; } JSONObject jo = new JSONObject(); try { jo.put("code", "1").put("message", "登录成功!"); } catch (JSONException e) { e.printStackTrace(); } return jo.toString(); } /** * 获取httpBody中的请求数据处理后并以JSON格式返回到httpBody中 测试@ReponseBody(无) @RequestBody(无) * * @param userLoginMap * @param request * @return * @throws IOException */ @RequestMapping(value = "/user/login2.json", method = RequestMethod.POST, headers = { "content-type=application/json" }) public void login(HttpServletRequest request, HttpServletResponse response) throws IOException { /** * 获取request body中的信息,用来代替spring中@RequestBody */ System.out.println(UserInfoTestController.getBodyString(request .getReader())); /** * 获取request header中的信息 */ UserInfoTestController.getHeaderString(request); JSONObject jo = new JSONObject(); try { jo.put("code", "1").put("message", "登录成功!"); } catch (JSONException e) { e.printStackTrace(); } /** * 返回头信息到body中,用来代替@ResponseBody */ response.setContentType("application/json; charset=UTF-8"); response.getWriter().print(jo.toString()); } /** * 获取http请求中body部分的信息,用来代替spring中@RequestBody * * @param br * @return */ public static String getBodyString(BufferedReader br) { String inputLine; String str = ""; try { while ((inputLine = br.readLine()) != null) { str += inputLine; } br.close(); } catch (IOException e) { System.out.println("IOException: " + e); } return str; } /** * 获取http请求中header中的参数 * * @param request */ public static void getHeaderString(HttpServletRequest request) { Enumeration<String> enu = request.getHeaderNames(); while (enu.hasMoreElements()) {// 以此取出头信息 String headerName = (String) enu.nextElement(); String headerValue = request.getHeader(headerName);// 取出头信息内容 System.out.println(headerName + ":" + headerValue); } }
模拟http请求:
HttpClient httpClient = new HttpClient(); StringRequestEntity requestEntity = new StringRequestEntity( "{\"account\":\"zzstxx\",\"password\":\"123456\",\"clientType\":\"3\"}", "application/json", "UTF-8"); PostMethod postMethod = new PostMethod( "http://127.0.0.1:81/user/login2.json"); postMethod.setRequestEntity(requestEntity); System.out.println(postMethod.getURI()); int statusCode = httpClient.executeMethod(postMethod); System.out.println("JSON:" + postMethod.getResponseBodyAsString()); System.out.println("获取的最终状态为:" + statusCode);
DefaultHttpClient httpClient = new DefaultHttpClient(); String url = "http://127.0.0.1:81/user/login2.json"; HttpPost httpPost = new HttpPost(url); // 设置请求的header参数 httpPost.addHeader("AppKey", "94kid09c9ig9k1loimjg012345123456"); httpPost.addHeader("Content-Type", "application/json;charset=utf-8"); // 设置请求的body参数 List<NameValuePair> nvps = new ArrayList<NameValuePair>(); nvps.add(new BasicNameValuePair("accid", "helloworld")); httpPost.setEntity(new UrlEncodedFormEntity(nvps, "utf-8")); // 执行请求 HttpResponse response = httpClient.execute(httpPost); // 打印执行结果 System.out.println(EntityUtils.toString(response.getEntity(), "utf-8"));