httpclient模拟post请求json封装表单数据

谁借莪1个温暖的怀抱¢ 2022-04-15 03:53 406阅读 0赞
  1. public static String httpPostWithJSON(String url) throws Exception {
  2. HttpPost httpPost = new HttpPost(url);
  3. CloseableHttpClient client = HttpClients.createDefault();
  4. String respContent = null;
  5. // json方式
  6. JSONObject jsonParam = new JSONObject();
  7. jsonParam.put("name", "admin");
  8. jsonParam.put("pass", "123456");
  9. StringEntity entity = new StringEntity(jsonParam.toString(),"utf-8");//解决中文乱码问题
  10. entity.setContentEncoding("UTF-8");
  11. entity.setContentType("application/json");
  12. httpPost.setEntity(entity);
  13. System.out.println();
  14. // 表单方式
  15. // List<BasicNameValuePair> pairList = new ArrayList<BasicNameValuePair>();
  16. // pairList.add(new BasicNameValuePair("name", "admin"));
  17. // pairList.add(new BasicNameValuePair("pass", "123456"));
  18. // httpPost.setEntity(new UrlEncodedFormEntity(pairList, "utf-8"));
  19. HttpResponse resp = client.execute(httpPost);
  20. if(resp.getStatusLine().getStatusCode() == 200) {
  21. HttpEntity he = resp.getEntity();
  22. respContent = EntityUtils.toString(he,"UTF-8");
  23. }
  24. return respContent;
  25. }
  26. public static void main(String[] args) throws Exception {
  27. String result = httpPostWithJSON("http://localhost:8080/hcTest2/Hc");
  28. System.out.println(result);
  29. }

post方式 就要考虑提交的表单内容怎么传输了。本文name和pass就是表单的值了。

封装表单属性可以用json也可以用传统的表单,如果是传统表单的话 要注意,也就是在上边代码注释那部分。用这种方式的话在servlet里也就是数据处理层可以通过request.getParameter(”string“)直接获取到属性值。就是相比json这种要简单一点,不过在实际开发中一般都是用json做数据传输的。用json的话有两种选择一个是阿里巴巴的fastjson还有一个就是谷歌的gson。fastjson相比效率比较高,gson适合解析有规律的json数据。博主这里用的是fastjson。还有用json的话在数据处理层要用流来读取表单属性,这就是相比传统表单多的一点内容。代码下边已经有了。

  1. public class HcServlet extends HttpServlet {
  2. private static final long serialVersionUID = 1L;
  3. protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  4. doPost(request, response);
  5. }
  6. protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  7. request.setCharacterEncoding("UTF-8");
  8. response.setContentType("text/html;charset=UTF-8");
  9. String acceptjson = "";
  10. User user = new User();
  11. BufferedReader br = new BufferedReader(new InputStreamReader(
  12. (ServletInputStream) request.getInputStream(), "utf-8"));
  13. StringBuffer sb = new StringBuffer("");
  14. String temp;
  15. while ((temp = br.readLine()) != null) {
  16. sb.append(temp);
  17. }
  18. br.close();
  19. acceptjson = sb.toString();
  20. if (acceptjson != "") {
  21. JSONObject jo = JSONObject.parseObject(acceptjson);
  22. user.setUsername(jo.getString("name"));
  23. user.setPassword(jo.getString("pass"));
  24. }
  25. request.setAttribute("user", user);
  26. request.getRequestDispatcher("/message.jsp").forward(request, response);
  27. }
  28. }

原文链接:https://www.cnblogs.com/Vdiao/p/5339487.html

发表评论

表情:
评论列表 (有 0 条评论,406人围观)

还没有评论,来说两句吧...

相关阅读