public static String getHttp(String url, String param) {
long startTime = System.currentTimeMillis();
String result = null;
HttpGet httpGet = null;
if (!StringUtils.isEmpty(param)) {
httpGet = new HttpGet(url + "?" + param);
} else {
httpGet = new HttpGet(url);
}
httpGet.setHeader("Content-Type", "text/plain;charset=utf-8");
RequestConfig config = RequestConfig.custom().setSocketTimeout(5000).setConnectTimeout(5000).build();
httpGet.setConfig(config);
HttpEntity resultEntity = null;
try {
// 执行请求并获取返回值
HttpClient httpClient = HttpClients.createDefault();
HttpResponse response = httpClient.execute(httpGet);
resultEntity = response.getEntity();
result = EntityUtils.toString(resultEntity, "UTF-8");
} catch (Exception e) {
ILogUtil.error(e.getMessage());
return result;
} finally {
if (httpGet != null) {
httpGet.abort();
}
try {
EntityUtils.consume(resultEntity);
} catch (final IOException e) {
}
}
long costTime = System.currentTimeMillis() - startTime;
ILogUtil.info("http请求>>>url名称:" + url + "?" + param + ">>>耗费时间:" + costTime + "ms");
return result;
}
HttpsUtil的getHttp方法
最新推荐文章于 2024-05-01 20:29:22 发布