public String getResult(String url) {
HttpClientBuilder builder = HttpClientBuilder.create();
CloseableHttpClient httpClient = builder.build();
HttpGet httpGet = new HttpGet(url);
CloseableHttpResponse httpResponse = null;
try {
httpResponse = httpClient.execute(httpGet);
if (httpResponse == null) {
httpGet.abort();
log.warn("响应为空: url" + url);
} else {
int statusCode = httpResponse.getStatusLine().getStatusCode();
if (statusCode != 203){
httpGet.abort();
log.warn("请求失败, 返回状态码为:" + httpResponse.getStatusLine().getStatusCode() + ", 并中断get请求" + url);
} else {
log.info("当前状态码为:" + statusCode);
// 有响应并且返回为203
try (ByteArrayOutputStream byteStream = new ByteArrayOutputStream();
InputStream inputStream = httpResponse.getEntity().getContent()) {
try {
if (inputStream == null) {
return null;
}
byte[] buffer = new byte[1024];
int len = 0;
while ((len = inputStream.read(buffer)) > 0) {
byteStream.write(buffer, 0, len);
}
} catch (IOException e) {
log.error("读取流异常", e);
}
return new String(byteStream.toByteArray());
} catch (IOException ex) {
log.error("关闭流异常", ex);
}
}
}
} catch (IOException e) {
e.printStackTrace();
}
return null;
}