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

来自cdnbest帮助文档
跳转至: 导航搜索
(创建页面,内容为“ public static void main(String[] args) { CloseableHttpClient httpClient = HttpClients.createDefault(); ObjectMapper objectMapper = new ObjectMapper(); Map<Str…”)
 
 
第1行: 第1行:
 
  public static void main(String[] args) {
 
  public static void main(String[] args) {
CloseableHttpClient httpClient = HttpClients.createDefault();
+
CloseableHttpClient httpClient = HttpClients.createDefault();
ObjectMapper objectMapper = new ObjectMapper();
+
ObjectMapper objectMapper = new ObjectMapper();
Map<String, String> map = new HashMap<String, String>();
+
Map<String, String> map = new HashMap<String, String>();
map.put("name", "admin");
+
map.put("name", "admin");
map.put("passwd", "kangle");
+
map.put("passwd", "kangle");
try {
+
try {
HttpPost http = new HttpPost("http://192.168.8.16:8088/api/admin/login");
+
HttpPost http = new HttpPost("http://192.168.8.16:8088/api/admin/login");
http.addHeader("Content-Type", "application/json;charset=utf-8");
+
http.addHeader("Content-Type", "application/json;charset=utf-8");
http.setHeader("cookie", "JSESSIONID=123123");
+
http.setHeader("cookie", "JSESSIONID=123123");
http.setEntity(new StringEntity(objectMapper.writeValueAsString(map), "utf-8"));
+
http.setEntity(new StringEntity(objectMapper.writeValueAsString(map), "utf-8"));
HttpResponse response = httpClient.execute(http);
+
HttpResponse response = httpClient.execute(http);
Header header = response.getHeaders("Set-Cookie")[0];
+
Header header = response.getHeaders("Set-Cookie")[0];
System.out.println(header);
+
System.out.println(header);
  
HttpGet httpGet = new HttpGet("http://192.168.8.16:8088/api/admin/info");
+
HttpGet httpGet = new HttpGet("http://192.168.8.16:8088/api/admin/info");
httpGet.setHeader("cookie", "JSESSIONID=" + header.getValue());
+
httpGet.setHeader("cookie", "JSESSIONID=" + header.getValue());
httpGet.setHeader("Content-Type", "application/x-www-form-urlencoded;charset=utf-8");
+
httpGet.setHeader("Content-Type", "application/x-www-form-urlencoded;charset=utf-8");
response = httpClient.execute(httpGet);
+
response = httpClient.execute(httpGet);
HttpEntity resEntity = response.getEntity();
+
HttpEntity resEntity = response.getEntity();
if (resEntity != null) {
+
if (resEntity != null) {
System.out.println(EntityUtils.toString(resEntity, "utf-8"));
+
System.out.println(EntityUtils.toString(resEntity, "utf-8"));
}
 
} catch (Exception e) {
 
e.printStackTrace();
 
 
}
 
}
 +
} catch (Exception e) {
 +
e.printStackTrace();
 
}
 
}
 +
}

2020年3月23日 (一) 04:01的最新版本

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(); }

}