需要的jar包
httpclient-4.5.jar
httpcore-4.4.1.jar
public static String findUrlHtml(String url, String encoding) {
String html = null;
CloseableHttpResponse response = null;
CloseableHttpClient httpclient = HttpClients.createDefault();
try {
HttpUriRequest request = new HttpGet(url);
response = httpclient.execute(request);
HttpEntity entity = response.getEntity();
if (entity != null) {
InputStream input = entity.getContent();
try {
html = IOUtils.toString(input, encoding);
} finally {
input.close();
}
}
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (response != null) {
response.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
return html;
}