激情久久久_欧美视频区_成人av免费_不卡视频一二三区_欧美精品在欧美一区二区少妇_欧美一区二区三区的

服務器之家:專注于服務器技術及軟件下載分享
分類導航

PHP教程|ASP.NET教程|JAVA教程|ASP教程|

服務器之家 - 編程語言 - JAVA教程 - java實現http請求工具類示例

java實現http請求工具類示例

2019-11-22 13:15Java教程網 JAVA教程

這篇文章主要介紹了java實現http請求工具類示例,需要的朋友可以參考下

通過http rest請求返回數據

 

復制代碼代碼如下:


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;

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

    /**
     * POST方式調用
     *
     * @param url
     * @param params 參數為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請求結果:" + 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    帶參數占位符的URL,例:http://****/User/user/center.aspx?_action=GetSimpleUserInfo&codes={0}&email={1}
     * @param params 參數值數組,需要與url中占位符順序對應
     * @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請求結果:" + 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請求結果:" + 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;
    }
}

 

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 精品一区二区三区电影 | 国产手机国产手机在线 | 国产羞羞视频在线观看 | a一级黄色大片 | 成人福利网 | 一级免费大片 | 精品69人人人人 | 日本高清在线免费 | 国外成人在线视频网站 | 国产一级在线免费观看 | 青青操精品 | 欧美精品国产综合久久 | 亚洲成人在线视频网站 | 成人午夜精品久久久久久久3d | 青热久思思 | 久久久久国产精品久久久久 | 少妇一级淫片免费放播放 | 亚洲国产精品久久久久制服红楼梦 | 国产亚洲美女精品久久久2020 | 久久91久久久久麻豆精品 | 欧美精品激情视频 | 最新中文字幕第一页视频 | 午夜精品成人 | 销魂美女一区二区 | 91在线看黄| 国产一区二区欧美 | 久久久久中精品中文字幕19 | 神马久久精品综合 | 久久久久久久久久久久免费 | 成人福利视频在 | 成年片黄色日本大片网站视频 | 中国a级黄色片 | 欧美高清第一页 | 91高清国产 | 国产精品免费一区二区三区都可以 | 精国品产一区二区三区有限公司 | 91精品国产91久久久久久不卞 | 国产精品久久久久久久久久久久久久久 | 亚洲午夜视频在线 | 日本在线不卡一区二区 | 日本a级免费 |