HTTPClient调用

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);
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值