1. 程式人生 > >httpclient 多執行緒高併發Get請求

httpclient 多執行緒高併發Get請求

public class HttpRequest {

    private static PoolingClientConnectionManager conMgr null;
    static {
        HttpParams params = new BasicHttpParams();
Integer CONNECTION_TIMEOUT = 1000//設定請求超時2秒鐘 根據業務調整
Integer SO_TIMEOUT = 1000//設定等待資料超時時間2秒鐘 根據業務調整
Long CONN_MANAGER_TIMEOUT = 500L//該值就是連線不夠用的時候等待超時時間,一定要設定,而且不能太大
params.setIntParameter(CoreConnectionPNames.CONNECTION_TIMEOUTCONNECTION_TIMEOUT);
params.setIntParameter(CoreConnectionPNames.SO_TIMEOUTSO_TIMEOUT);
params.setLongParameter(ClientPNames.CONN_MANAGER_TIMEOUTCONN_MANAGER_TIMEOUT);
params.setBooleanParameter(CoreConnectionPNames.STALE_CONNECTION_CHECK
, true);
conMgr new PoolingClientConnectionManager();
conMgr.setMaxTotal(2000);
conMgr.setDefaultMaxPerRoute(conMgr.getMaxTotal());
}

    public static String get(String urlString param) {

        DefaultHttpClient httpClient = new DefaultHttpClient(conMgr);
//        httpClient.setParams(params);
httpClient.setHttpRequestRetryHandler(new 
DefaultHttpRequestRetryHandler(0, false));
HttpResponse httpResponse = null;
// 傳送get請求
try {
            // get方法傳送http請求
HttpGet get = new HttpGet(url + URLEncoder.encode(param"UTF-8"));
//            HttpGet get = new HttpGet(url + param);
//            get.setHeader("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8");
//            get.setHeader("Accept-Encoding", "gzip, deflate, sdch");
//            get.setHeader("Accept-Language", "zh-CN,zh;q=0.8,en-US;q=0.6,en;q=0.4");
//            get.setHeader("Cache-Control", "max-age=0");
//            get.setHeader("Connection", "keep-alive");
//            get.setHeader("Content-Type", "text/xml;charset=utf-8");
//            get.setHeader("Cookie", "BDUSS=EZZa1RmTVZWU0NqZ2VuM1RNdVhuYjR4QTkzbTNaMGRrNXladmFidFRwZHZmQ1pZQVFBQUFBJCQAAAAAAAAAAAEAAABNtEoNcWlhbmppY2hlbmdhYmMAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAG~v~ldv7~5XO; BAIDUID=50970119FD0148A9DC0B168BC1DF574C:FG=1; PSTM=1476958371; BDRCVFR[nXyXJys849T]=mk3SLVN4HKm; BIDUPSID=D09F46D9DB5776482C08FA93075CE076; MCITY=-289%3A; BDRCVFR[feWj1Vr5u3D]=I67x6TjHwwYf0; PSINO=5; H_PS_PSSID=1429_19036_18240_17949_21109_17001_20593_21377_21189_21372");
//            get.setHeader("Host", "180.97.33.90");
//            get.setHeader("Upgrade-Insecure-Requests", "1");
get.setHeader("User-Agent""Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Ubuntu Chromium/51.0.2704.79 Chrome/51.0.2704.79 Safari/537.36");
System.out.println("執行get請求, uri: " + get.getURI());
httpResponse = httpClient.execute(get);
// response實體
HttpEntity entity = httpResponse.getEntity();
            if (null != entity) {
                String response = EntityUtils.toString(entity);
                int statusCode = httpResponse.getStatusLine().getStatusCode();
//                System.out.println("響應狀態碼:" + statusCode);
//                System.out.println("響應內容:" + response);
if (statusCode == HttpStatus.SC_OK) {
                    // 成功
return response;
else {
                    return null;
}
            }
        } catch (IOException e) {
            e.printStackTrace();
System.out.println("httpclient請求失敗");
            return null;
finally {
            if (httpResponse != null) {
                try {
                    EntityUtils.consume(httpResponse.getEntity())//會自動釋放連線
catch (IOException e) {
                    e.printStackTrace();
}
            }
        }
        return null;
}