https://www.cnblogs.com/liujisong2014221/articles/8269572.html
转自这里
1 //访问地址 2 String uri = ""; 3 //创建client对象 4 HttpClient client = HttpClients.createDefault(); 5 //创建post对象 6 HttpPost post = new HttpPost(uri); 7 8 //请求参数 9 List<NameValuePair> paras = new ArrayList<NameValuePair>(); 10 //键值对 11 paras.add(new BasicNameValuePair("username", "admin")); 12 paras.add(new BasicNameValuePair("password", "123456")); 13 14 //创建entity对象 15 UrlEncodedFormEntity entity = new UrlEncodedFormEntity(paras); 16 17 //把entity对象放入post请求中 18 post.setEntity(entity); 19 //执行请求 20 HttpResponse resp = client.execute(post); 21 //获取服务端返回的数据 22 HttpEntity en = resp.getEntity(); 23 //把en对象转为字符串 24 String result = EntityUtils.toString(en); 25 System.out.println("结果:"+result);
拓展httpurlConnection