优化这段代码@PostMapping("/login") @ResponseBody @PassToken public Object login(String username,String password) throws IOException { String url="http://42.177.95.222:9202/platform/yugang/task/getToken"; HttpClient client = HttpClients.createDefault(); //默认post请求 HttpPost post = new HttpPost(url); //拼接多参数 JSONObject json = new JSONObject(); json.put("username", "渔业协会"); json.put("password", "Yuye!@#qwe"); post.addHeader("Content-type", "application/json; charset=utf-8"); post.setHeader("Accept", "application/json"); post.setEntity(new StringEntity(json.toString(), Charset.forName("utf-8"))); HttpResponse httpResponse = client.execute(post); HttpEntity entity = httpResponse.getEntity(); String s = EntityUtils.toString(entity); JSONObject jo = JSONObject.parseObject(s); String token = jo.getJSONObject("data").getString("token"); System.out.println("dsadasdasdsadasd"+token); return verify(token); } public Object verify(String token) throws IOException { String url="http://42.177.95.222:9202/platform/yugang/task/verify"; HttpClient client = HttpClients.createDefault(); //默认post请求 HttpPost post = new HttpPost(url); //拼接多参数 JSONObject json = new JSONObject(); json.put("token",token); post.addHeader("Content-type", "application/json; charset=utf-8"); post.setHeader("Accept", "application/json"); post.setEntity(new StringEntity(json.toString(), Charset.forName("utf-8"))); HttpResponse httpResponse = client.execute(post); HttpEntity entity = httpResponse.getEntity(); String s = EntityUtils.toString(entity); JSONObject jo = JSONObject.parseObject(s); return jo; }
时间: 2024-01-12 17:03:41 浏览: 153
这段代码的优化点有以下几个:
1. 将重复的代码抽取成公共方法,避免代码冗余。
2. 使用连接池管理 HttpClient 的连接,提高连接的复用率和性能。
3. 使用 try-with-resources 语句关闭 HttpClient 和 HttpEntity 对象,避免资源泄漏。
下面是代码优化后的示例:
```
private static final String BASE_URL = "http://42.177.95.222:9202/platform/yugang/task";
private CloseableHttpClient httpClient = HttpClientBuilder.create()
.setMaxConnTotal(200)
.setMaxConnPerRoute(100)
.build();
@PostMapping("/login")
@ResponseBody
@PassToken
public Object login(String username, String password) throws IOException {
String token = getToken();
System.out.println("dsadasdasdsadasd" + token);
return verify(token);
}
private String getToken() throws IOException {
String url = BASE_URL + "/getToken";
HttpPost post = new HttpPost(url);
post.addHeader("Content-type", "application/json; charset=utf-8");
post.setHeader("Accept", "application/json");
JSONObject json = new JSONObject();
json.put("username", "渔业协会");
json.put("password", "Yuye!@#qwe");
post.setEntity(new StringEntity(json.toString(), Charset.forName("utf-8")));
try (CloseableHttpResponse response = httpClient.execute(post)) {
HttpEntity entity = response.getEntity();
if (entity != null) {
String s = EntityUtils.toString(entity);
JSONObject jo = JSONObject.parseObject(s);
return jo.getJSONObject("data").getString("token");
}
return null;
}
}
private JSONObject verify(String token) throws IOException {
String url = BASE_URL + "/verify";
HttpPost post = new HttpPost(url);
post.addHeader("Content-type", "application/json; charset=utf-8");
post.setHeader("Accept", "application/json");
JSONObject json = new JSONObject();
json.put("token", token);
post.setEntity(new StringEntity(json.toString(), Charset.forName("utf-8")));
try (CloseableHttpResponse response = httpClient.execute(post)) {
HttpEntity entity = response.getEntity();
if (entity != null) {
String s = EntityUtils.toString(entity);
return JSONObject.parseObject(s);
}
return null;
}
}
```
在优化后的代码中,我们将 HttpClient 对象的创建和关闭都放在了方法外部,使用连接池管理 HttpClient 的连接,提高连接的复用率和性能。同时,我们也将 getToken 和 verify 方法抽取出来,避免了代码冗余。最后,我们使用了 try-with-resources 语句来关闭 HttpClient 和 HttpEntity 对象,避免了资源泄漏。
阅读全文