HTTP get、post 中請求json與map傳參格式

import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.apache.http.HttpEntity;
import org.apache.http.HttpStatus;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.utils.URIBuilder;
import org.apache.http.conn.HttpHostConnectException;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.alibaba.fastjson.JSON;

public class HttpUtils {

	
	private transient static Logger log = LoggerFactory.getLogger(HttpUtils.class);
	
	
	public static String doGet(String url, Map<String,String> param){
		//創建HttpClient對象
		CloseableHttpClient httpclient = HttpClients.createDefault();
        String resultString = "";
        CloseableHttpResponse httpResponse = null;
        try {
        	//創建uri
        	URIBuilder builder = new URIBuilder(url);
            if(param!=null && !param.isEmpty()){
            	for(String key :param.keySet()){
            		builder.addParameter(key, param.get(key));
            	}
            }
            URI uri = builder.build();
            // 創建httpGet請求
            HttpGet httpGet = new HttpGet(uri);
            
            // 開始執行http請求
            long startTime = System.currentTimeMillis();
            httpResponse = httpclient.execute(httpGet);
            long endTime = System.currentTimeMillis();
            
            // 獲得響應狀態碼
            int statusCode = httpResponse.getStatusLine().getStatusCode();  
            log.info("調用API花費時間(單位:毫秒):" + (endTime - startTime));
            
            // 取出應答字符串
            HttpEntity httpEntity = httpResponse.getEntity();
            resultString = EntityUtils.toString(httpEntity,Charset.forName("UTF-8"));
            // 去掉返回結果中的"\r"字符,否則會在結果字符串後面顯示一個小方格
            resultString.replaceAll("\r", "");
            
            // 判斷返回狀態是否爲200
            if (statusCode != HttpStatus.SC_OK) {
            	throw new RuntimeException(String.format("\n\tStatus:%s\n\tError Message:%s", statusCode,resultString));
            }
        } catch (ClientProtocolException e) {
            log.error(e.getMessage(), e);
        } catch (IOException e) {
            log.error(e.getMessage(), e);
        } catch (URISyntaxException e) {
        	log.error(e.getMessage(), e);
		} finally{
            try {
            	if(httpResponse != null){
            		httpResponse.close();
            	}
            	httpclient.close();
			} catch (IOException e) {
				log.error(e.getMessage(), e);
			}
        }
        return resultString;
    }
	
	public static String doGet(String url){
		return doGet(url,null);
	}
	
	public static String doPost(String url, Map<String,Object> param) throws HttpHostConnectException,IOException{
		//創建HttpClient對象
        CloseableHttpClient httpclient = HttpClients.createDefault();
        String resultString = "";
        CloseableHttpResponse httpResponse = null;
        try {
        	// 創建HttpPost對象
            HttpPost httpPost = new HttpPost(url);
            
            if(param!=null && !param.isEmpty()){
            	List<NameValuePair> params = new ArrayList<NameValuePair>();
            	for(String key :param.keySet()){
            		params.add(new BasicNameValuePair(key, param.get(key)==null?"":param.get(key).toString()));
            	}
            	httpPost.setEntity(new UrlEncodedFormEntity(params, Charset.forName("UTF-8")));
            }
            
            //設置請求和傳輸超時時間
            RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(120000).setConnectTimeout(120000).build();
            httpPost.setConfig(requestConfig);
            
            // 開始執行http請求
            long startTime = System.currentTimeMillis(); 
            httpResponse = httpclient.execute(httpPost);
            long endTime = System.currentTimeMillis();
            
            // 獲得響應狀態碼
            int statusCode = httpResponse.getStatusLine().getStatusCode();  
            log.info("調用API花費時間(單位:毫秒):" + (endTime - startTime));
            
            // 取出應答字符串
            HttpEntity httpEntity = httpResponse.getEntity();
            resultString = EntityUtils.toString(httpEntity,Charset.forName("UTF-8"));
            
            // 判斷返回狀態是否爲200
            if (statusCode != HttpStatus.SC_OK) {
            	throw new RuntimeException(String.format("\n\tStatus:%s\n\tError Message:%s", statusCode,resultString));
            } 
            
            //對返回結果進行非空校驗
            if (StringUtils.isBlank(resultString)||StringUtils.equals(StringUtils.trim(resultString), "null")) {
                log.error("被調用系統響應參數:{}",resultString);
                resultString = "{'code':100005,'msg':'響應不合法!'}";
            }
        } catch (ClientProtocolException e) {
            log.error(e.getMessage(), e);
        } finally{
            try {
            	if(httpResponse != null){
            		httpResponse.close();
            	}
            	httpclient.close();
			} catch (IOException e) {
				log.error(e.getMessage(), e);
			}
        }
		return resultString;
    }

	public static String doJsonPost(String url, Map<String, Object> param) {
		String resultString = "";
		if (param != null && !param.isEmpty()) {
			String json = JSON.toJSONString(param);
			resultString = doJsonPost(url, json);
		} else {
			resultString = doJsonPost(url, "");
		}
		return resultString;
	}

	public static String doJsonPost(String url, String json) {
		// 創建HttpClient對象
		CloseableHttpClient httpclient = HttpClients.createDefault();
		String resultString = "";
		CloseableHttpResponse httpResponse = null;
		try {
			// 創建HttpPost對象
			HttpPost httpPost = new HttpPost(url);
			httpPost.setEntity(new StringEntity(json,
					ContentType.APPLICATION_JSON));

			// 開始執行http請求
			long startTime = System.currentTimeMillis();
			httpResponse = httpclient.execute(httpPost);
			long endTime = System.currentTimeMillis();

			// 獲得響應狀態碼
			int statusCode = httpResponse.getStatusLine().getStatusCode();
			log.info("調用API 花費時間(單位:毫秒):" + (endTime - startTime));

			// 取出應答字符串
			HttpEntity httpEntity = httpResponse.getEntity();
			resultString = EntityUtils.toString(httpEntity,
					Charset.forName("UTF-8"));

			// 判斷返回狀態是否爲200
			if (statusCode != HttpStatus.SC_OK) {
				throw new RuntimeException(String.format(
						"\n\tStatus:%s\n\tError Message:%s", statusCode,
						resultString));
			}
		} catch (ClientProtocolException e) {
			log.error(e.getMessage(), e);
		} catch (IOException e) {
			log.error(e.getMessage(), e);
		} finally {
			try {
				if (httpResponse != null) {
					httpResponse.close();
				}
				httpclient.close();
			} catch (IOException e) {
				log.error(e.getMessage(), e);
			}
		}
		return resultString;
	}
	
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章