package cn.ce.ebiz.manager.util;
import java.io.IOException;
import org.apache.http.HttpEntity;
import org.apache.http.StatusLine;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpUriRequest;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class HttpClientUtils {
private static Logger logger = LoggerFactory.getLogger(HttpClientUtils.class);
public static final String temp_domain="http://p1510220001.make.yun300.cn";
public static final String APPLICATION_JSON = "application/json";
public static final String CONTENT_TYPE_TEXT_JSON = "text/json";
public static String execute(HttpUriRequest request){
CloseableHttpClient client = HttpClients.createDefault();
String responseStr =null;
CloseableHttpResponse response = null;
if(client!=null){
try {
response = client.execute(request);
StatusLine status = response.getStatusLine();
Integer code = status.getStatusCode();
if(code==200){
HttpEntity entity = response.getEntity();
responseStr = EntityUtils.toString(entity);
}
} catch (ClientProtocolException e) {
logger.error("HttpClientUtils--execute:",e);
} catch (IOException e) {
logger.error("HttpClientUtils--execute:",e);
}finally{
try {
response.close();
} catch (IOException e1) {
e1.printStackTrace();
}
// 关闭连接,释放资源
try {
client.close();
} catch (IOException e) {
logger.error("HttpClientUtils--execute:",e);
}
}
}
return responseStr;
}
}
HttpGet
HttpGet request = new HttpGet(getServiceListUrl+"?custId="+custId+"&websiteCode="+websiteCode);
String reponseStr = HttpClientUtils.execute(request);
HttpPost
String paralist= org.apache.commons.lang3.StringUtils.join(replyIdList, ",");
List<NameValuePair> tempParams = new ArrayList<NameValuePair>();
tempParams.add(new BasicNameValuePair("param", "{\"memberCodes\":["+"\""+paralist.replace(",", "\",\"")+"\""+"]}"));
UrlEncodedFormEntity uefEntity = null;
HttpPost getInfo = new HttpPost(memberInfoAddress);
uefEntity = new UrlEncodedFormEntity(tempParams, "UTF-8");
getInfo.setEntity(uefEntity);
String reponseStr = HttpClientUtils.execute(getInfo);