/*** 通用get方法
*@paramaddr
*@paramparams
*@return
*/
public static ServiceResult sendGet(String addr, Mapparams) {
String res= "error";
ServiceResult sr= new ServiceResult(false, res);
CloseableHttpClient httpclient=HttpClients.createDefault();
HttpGet httpGet= newHttpGet(addr);
List values =Lists.newArrayList();for (Map.Entryentry : params.entrySet()) {
values.add(newBasicNameValuePair(entry.getKey(), entry.getValue()));
}
String str= "";
UrlEncodedFormEntity uefEntity;try{
str= EntityUtils.toString(newUrlEncodedFormEntity(values, Consts.UTF_8));
LOG.warn("executing request " + addr + "?" +str);//设置请求和传输超时时间
RequestConfig requestConfig =RequestConfig.custom().setSocketTimeout(READTIMEOUT).setConnectTimeout(
CONNECTTIMEOUT).build();
httpGet.setConfig(requestConfig);
CloseableHttpResponse response=httpclient.execute(httpGet);try{if (HttpStatus.SC_OK ==response.getStatusLine().getStatusCode()) {
sr.setData(true);
sr.setSucceed(true);
}else{
sr.setSucceed(false);
sr.setData(false);
}
HttpEntity entity=response.getEntity();if (entity != null) {
res= EntityUtils.toString(entity, "UTF-8");
LOG.warn(res);
}
}finally{
response.close();
}
}catch(ClientProtocolException e) {
e.printStackTrace();
LOG.warn("", e);
}catch(UnsupportedEncodingException e) {
LOG.warn("", e);
}catch(IOException e) {
LOG.warn("", e);
}finally{//关闭连接,释放资源
try{
httpclient.close();
}catch(IOException e) {
LOG.warn("", e);
}
}returnsr;
}