<--httpclient依赖-->
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5.12</version>
</dependency>
测试:
@Test
void Test() {
//开流
CloseableHttpClient client = null;
HttpGet httpGet = null;
CloseableHttpResponse response = null ;
try {
//打开网页浏览器
client = HttpClientBuilder.create().build();
//输入需要的网站
httpGet = new HttpGet("https://www.baidu.com/");
//敲回车执行
response = client.execute(httpGet);
//网站响应
String result = EntityUtils.toString(response.getEntity(), "utf-8");
//输入内容
System.out.println(result);
} catch (IOException e) {
e.printStackTrace();
}finally {
//关流
try {
if (response!=null) {
response.close();
}
if (httpGet!=null){
httpGet.releaseConnection();
}
if (client!=null){
client.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
运行效果:得到代码
使用工具测试代码