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

好长时间不更博了,主要肚子里没什么好墨水,哈哈。废话不说上代码。
 1     public static String httpPostWithJSON(String url) throws Exception {
 2 
 3         HttpPost httpPost = new HttpPost(url);
 4         CloseableHttpClient client = HttpClients.createDefault();
 5         String respContent = null;
 6         
 7 //        json方式
 8         JSONObject jsonParam = new JSONObject();  
 9         jsonParam.put("name", "admin");
10         jsonParam.put("pass", "123456");
11         StringEntity entity = new StringEntity(jsonParam.toString(),"utf-8");//解决中文乱码问题    
12         entity.setContentEncoding("UTF-8");    
13         entity.setContentType("application/json");    
14         httpPost.setEntity(entity);
15         System.out.println();
16         
17     
18 //        表单方式
19 //        List<BasicNameValuePair> pairList = new ArrayList<BasicNameValuePair>(); 
20 //        pairList.add(new BasicNameValuePair("name", "admin"));
21 //        pairList.add(new BasicNameValuePair("pass", "123456"));
22 //        httpPost.setEntity(new UrlEncodedFormEntity(pairList, "utf-8"));   
23         
24         
25         HttpResponse resp = client.execute(httpPost);
26         if(resp.getStatusLine().getStatusCode() == 200) {
27             HttpEntity he = resp.getEntity();
28             respContent = EntityUtils.toString(he,"UTF-8");
29         }
30         return respContent;
31     }
32 
33     
34     public static void main(String[] args) throws Exception {
35         String result = httpPostWithJSON("http://localhost:8080/hcTest2/Hc");
36         System.out.println(result);
37     }

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        
 4     protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
 5         doPost(request, response);
 6     }
 7 
 8     
 9     protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
10         
11         request.setCharacterEncoding("UTF-8");  
12         response.setContentType("text/html;charset=UTF-8");  
13         String acceptjson = "";  
14         User user = new User();
15         BufferedReader br = new BufferedReader(new InputStreamReader(  
16                 (ServletInputStream) request.getInputStream(), "utf-8"));  
17         StringBuffer sb = new StringBuffer("");  
18         String temp;  
19         while ((temp = br.readLine()) != null) {  
20             sb.append(temp);  
21         }  
22         br.close();  
23         acceptjson = sb.toString();  
24         if (acceptjson != "") {  
25             JSONObject jo = JSONObject.parseObject(acceptjson);
26             user.setUsername(jo.getString("name"));
27             user.setPassword(jo.getString("pass"));
28         }  
29         
30         request.setAttribute("user", user);
31         request.getRequestDispatcher("/message.jsp").forward(request, response);
32     }
33 }

代码比较简陋,只是用于测试。希望能够有所收获。

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值