国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Java > 正文

java實現(xiàn)http請求工具類示例

2019-11-26 15:30:16
字體:
供稿:網(wǎng)友

通過http rest請求返回數(shù)據(jù)

復制代碼 代碼如下:

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.ResponseHandler;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.BasicResponseHandler;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.impl.conn.tsccm.ThreadSafeClientConnManager;

import java.io.IOException;
import java.text.MessageFormat;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * 分裝一個http請求的工具類
 *
 * @author 顧煒【guwei】 on 14-4-22.下午3:17
 */
public class HttpClientUtils {

    private static final Log log = LogFactory.getLog(HttpClientUtils.class);
    /**
     * 初始化HttpClient
     */
    private static HttpClient httpClient = null;

    /**
     * 生產(chǎn)HttpClient實例
     * 公開,靜態(tài)的工廠方法,需要使用時才去創(chuàng)建該單體
     *
     * @return
     */
    public static HttpClient getHttpClient() {
        if (httpClient == null) {
            httpClient = new DefaultHttpClient(new ThreadSafeClientConnManager());
        }
        return httpClient;
    }

    /**
     * POST方式調(diào)用
     *
     * @param url
     * @param params 參數(shù)為NameValuePair鍵值對對象
     * @return 響應字符串
     * @throws java.io.UnsupportedEncodingException
     */
    public static String executeByPOST(String url, List<NameValuePair> params) {
        HttpClient httpclient = getHttpClient();

        HttpPost post = new HttpPost(url);


        ResponseHandler<String> responseHandler = new BasicResponseHandler();
        String responseJson = null;
        try {
            if (params != null) {
                post.setEntity(new UrlEncodedFormEntity(params));
            }
            responseJson = httpclient.execute(post, responseHandler);
            log.info("HttpClient POST請求結(jié)果:" + responseJson);
        } catch (ClientProtocolException e) {
            e.printStackTrace();
            log.info("HttpClient POST請求異常:" + e.getMessage());
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            httpclient.getConnectionManager().closeExpiredConnections();
            httpclient.getConnectionManager().closeIdleConnections(30, TimeUnit.SECONDS);
        }
        return responseJson;
    }

    /**
     * Get方式請求
     *
     * @param url    帶參數(shù)占位符的URL,例:http://****/User/user/center.aspx?_action=GetSimpleUserInfo&codes={0}&email={1}
     * @param params 參數(shù)值數(shù)組,需要與url中占位符順序?qū)?BR>     * @return 響應字符串
     * @throws java.io.UnsupportedEncodingException
     */
    public static String executeByGET(String url, Object[] params) {
        HttpClient httpclient = getHttpClient();

        String messages = MessageFormat.format(url, params);

        HttpGet get = new HttpGet(messages);

        ResponseHandler<String> responseHandler = new BasicResponseHandler();
        String responseJson = null;
        try {
            responseJson = httpclient.execute(get, responseHandler);
            log.info("HttpClient GET請求結(jié)果:" + responseJson);
        } catch (ClientProtocolException e) {
            e.printStackTrace();
            log.info("HttpClient GET請求異常:" + e.getMessage());
        } catch (IOException e) {
            e.printStackTrace();
            log.info("HttpClient GET請求異常:" + e.getMessage());
        } finally {
            httpclient.getConnectionManager().closeExpiredConnections();
            httpclient.getConnectionManager().closeIdleConnections(30, TimeUnit.SECONDS);
        }
        return responseJson;
    }

    /**
     * @param url
     * @return
     */
    public static String executeByGET(String url) {
        HttpClient httpclient = getHttpClient();

        HttpGet get = new HttpGet(url);

        ResponseHandler<String> responseHandler = new BasicResponseHandler();
        String responseJson = null;
        try {
            responseJson = httpclient.execute(get, responseHandler);
            log.info("HttpClient GET請求結(jié)果:" + responseJson);
        } catch (ClientProtocolException e) {
            e.printStackTrace();
            log.info("HttpClient GET請求異常:" + e.getMessage());
        } catch (IOException e) {
            e.printStackTrace();
            log.info("HttpClient GET請求異常:" + e.getMessage());
        } finally {
            httpclient.getConnectionManager().closeExpiredConnections();
            httpclient.getConnectionManager().closeIdleConnections(30, TimeUnit.SECONDS);
        }
        return responseJson;
    }
}

發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 武强县| 南安市| 汝州市| 泰和县| 浦北县| 普兰县| 翁牛特旗| 芦山县| 如皋市| 盱眙县| 华蓥市| 文安县| 阳朔县| 彭阳县| 长岛县| 永德县| 土默特右旗| 高要市| 林周县| 时尚| 墨玉县| 北辰区| 芦山县| 隆化县| 乌审旗| 咸阳市| 噶尔县| 洪洞县| 洪江市| 浮山县| 德清县| 武宣县| 新龙县| 中牟县| 榕江县| 合川市| 锡林浩特市| 潮安县| 莱阳市| 汝城县| 永靖县|