use of com.dtstack.taier.common.exception.DtCenterDefException in project Taier by DTStack.
the class PoolHttpClient method get.
/**
* @param url
* @param cookies
* @param headers
* @param dealWithRespBody true :deal with RespBody,false: Don't deal with RespBody
* @return
*/
public static String get(String url, Map<String, Object> cookies, Map<String, Object> headers, Boolean dealWithRespBody) {
HttpGet httpGet = new HttpGet(url);
setConfig(httpGet);
if (cookies != null && cookies.size() > 0) {
httpGet.setHeader("Cookie", getCookieFormate(cookies));
}
if (headers != null && !headers.isEmpty()) {
for (Map.Entry<String, Object> entry : headers.entrySet()) {
httpGet.addHeader(entry.getKey(), String.valueOf(entry.getValue()));
}
}
// true :默认逻辑处理 false:直接返回返回结果 不对response进行任何处理 直接返回
try (CloseableHttpResponse response = httpClient.execute(httpGet)) {
HttpEntity entity = response.getEntity();
String result = EntityUtils.toString(entity, Charsets.UTF_8);
if (BooleanUtils.isTrue(dealWithRespBody)) {
String respBody = null;
int statusCode = response.getStatusLine().getStatusCode();
if (statusCode == HttpStatus.SC_OK) {
respBody = result;
} else if (statusCode == HttpStatus.SC_UNAUTHORIZED) {
throw new DtCenterDefException("登陆状态失效");
} else {
logger.error("request url:{} fail:{}", url, result);
}
return respBody;
} else {
return result;
}
} catch (Exception e) {
logger.error("url:{}--->http request error:{}", url, e);
}
return null;
}
Aggregations