“Java登陆后获取管理员信息示例api”的版本间的差异

来自cdnbest帮助文档
跳转至: 导航搜索
(创建页面,内容为“ public static void main(String[] args) { CloseableHttpClient httpClient = HttpClients.createDefault(); ObjectMapper objectMapper = new ObjectMapper(); Map<Str…”)
(没有差异)

2020年3月23日 (一) 04:00的版本

public static void main(String[] args) {

CloseableHttpClient httpClient = HttpClients.createDefault(); ObjectMapper objectMapper = new ObjectMapper(); Map<String, String> map = new HashMap<String, String>(); map.put("name", "admin"); map.put("passwd", "kangle"); try { HttpPost http = new HttpPost("http://192.168.8.16:8088/api/admin/login"); http.addHeader("Content-Type", "application/json;charset=utf-8"); http.setHeader("cookie", "JSESSIONID=123123"); http.setEntity(new StringEntity(objectMapper.writeValueAsString(map), "utf-8")); HttpResponse response = httpClient.execute(http); Header header = response.getHeaders("Set-Cookie")[0]; System.out.println(header);

HttpGet httpGet = new HttpGet("http://192.168.8.16:8088/api/admin/info"); httpGet.setHeader("cookie", "JSESSIONID=" + header.getValue()); httpGet.setHeader("Content-Type", "application/x-www-form-urlencoded;charset=utf-8"); response = httpClient.execute(httpGet); HttpEntity resEntity = response.getEntity(); if (resEntity != null) { System.out.println(EntityUtils.toString(resEntity, "utf-8")); } } catch (Exception e) { e.printStackTrace(); } }