国产片侵犯亲女视频播放_亚洲精品二区_在线免费国产视频_欧美精品一区二区三区在线_少妇久久久_在线观看av不卡

服務(wù)器之家:專注于服務(wù)器技術(shù)及軟件下載分享
分類導(dǎo)航

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

服務(wù)器之家 - 編程語言 - JAVA教程 - java實(shí)現(xiàn)http請求工具類示例

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

2019-11-22 13:15Java教程網(wǎng) JAVA教程

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

通過http rest請求返回?cái)?shù)據(jù)

 

復(fù)制代碼代碼如下:


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;

/**
 * 分裝一個(gè)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實(shí)例
     * 公開,靜態(tài)的工廠方法,需要使用時(shí)才去創(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 響應(yīng)字符串
     * @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ū)?yīng)
     * @return 響應(yīng)字符串
     * @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;
    }
}

 

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 欧美久久综合 | 欧美区国产区 | 久久久久久久国产精品免费播放 | 成年人av在线 | 久久久久国产一级毛片高清片 | 97久久精品 | 亚洲三级视频 | 成人性大片免费观看网站 | 青青草在线视频免费观看 | 亚洲视频在线播放 | av在线免费观看网站 | 免费视频黄 | 欧美精品一二区 | 亚洲高清资源 | 亚洲一区中文字幕在线观看 | 成人高清在线 | 国产精品一卡二卡三卡 | 成视频年人免费看黄网站 | 国产一区二区三区四区在线观看 | 91伊人| 免费观看电视在线高清视频 | 日韩视频一区二区 | 91精品国产综合久久久久 | 国产精品美女久久久久aⅴ国产馆 | 91新视频 | 国产免费一区二区三区 | 不卡一区| 免费在线一区二区 | 中文字幕高清一区 | 久久视频在线看 | www.av在线 | 国产精品久久久久久久久久三级 | 日韩中文字幕在线观看视频 | 偷拍自拍网 | 成人午夜免费视频 | 中文字幕一区二区三区不卡 | 99国产精品99久久久久久 | 日韩欧美手机在线 | 91视频.com | 群p在线观看 | 黄色免费网站在线观看 |