DefaultHttpClient显示被弃用解决办法

DefaultHttpClient被弃用可以用HttpClient来代替。注:HttpClient不是java.net.http里面的那个,而是org.apach.http.client.HttpClient。

DefaultHttpClient代码

@GetMapping("/test/testGet")

public String testGet() throws IOException {

DefaultHttpClient client = new DefaultHttpClient(new PoolingClientConnectionManager());

HttpGet httpGet = new HttpGet("http://localhost:7500/pfc/dict/bounceType");

httpGet.setHeader("Content-Type", "application/json");

CloseableHttpResponse response = client.execute(httpGet);

HttpEntity httpEntity = response.getEntity();

String result = EntityUtils.toString(httpEntity, "UTF-8");

return result;

}

HttpClient代码

@GetMapping("/test/testGet")

public String testGet() throws IOException {

HttpClient client = HttpClientBuilder.create().build();

HttpGet httpGet = new HttpGet("http://localhost:7500/pfc/dict/bounceType");

httpGet.setHeader("Content-Type", "application/json");

HttpResponse response = client.execute(httpGet);

HttpEntity httpEntity = response.getEntity();

String result = EntityUtils.toString(httpEntity, "UTF-8");

return result;

}

代码HttpClientBuilder.create().build()的返回类型其实是CloseableHttpClient,但是因为CloseableHttpClient继承于HttpClient,所以这里用HttpClient接收也没有问题。

还有就是执行请求得到远程响应类型为HttpResponse而不是CloseableHttpResponse

友情链接:
Copyright © 2022 86年世界杯_世界杯预选赛阿根廷 - fjyfzz.com All Rights Reserved.