com.careerly.utils.HttpClientUtils.java Source code

Java tutorial

Introduction

Here is the source code for com.careerly.utils.HttpClientUtils.java

Source

/*
 * Copyright 2013 Qunar.com All right reserved. This software is the confidential and proprietary information of
 * Qunar.com ("Confidential Information"). You shall not disclose such Confidential Information and shall use it only in
 * accordance with the terms of the license agreement you entered into with Qunar.com.
 */
package com.careerly.utils;

import org.apache.commons.lang.StringUtils;
import org.apache.http.*;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.HttpResponseException;
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.conn.scheme.PlainSocketFactory;
import org.apache.http.conn.scheme.Scheme;
import org.apache.http.conn.scheme.SchemeRegistry;
import org.apache.http.conn.ssl.SSLSocketFactory;
import org.apache.http.entity.StringEntity;
import org.apache.http.entity.mime.MultipartEntity;
import org.apache.http.entity.mime.content.FileBody;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.impl.conn.PoolingClientConnectionManager;
import org.apache.http.impl.conn.tsccm.ThreadSafeClientConnManager;
import org.apache.http.message.BasicHeader;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.params.CoreConnectionPNames;
import org.apache.http.params.HttpParams;
import org.apache.http.params.HttpProtocolParams;
import org.apache.http.params.SyncBasicHttpParams;
import org.apache.http.protocol.HTTP;
import org.apache.http.util.EntityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.net.ssl.SSLContext;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;
import java.io.File;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.SocketTimeoutException;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

/**
 * ??http
 */
public class HttpClientUtils {
    /**
     * ???httpEntity?
     *
     * @version v1.0.0
     * @see
     * @since 2013-8-2 ?5:21:59
     */
    private static class CharsetableResponseHandler implements ResponseHandler<String> {

        private String defaultEncoding;

        public CharsetableResponseHandler(String defaultEncoding) {
            this.defaultEncoding = defaultEncoding;
        }

        @Override
        public String handleResponse(HttpResponse response) throws ClientProtocolException, IOException {
            StatusLine statusLine = response.getStatusLine();
            HttpEntity entity = response.getEntity();
            if (statusLine.getStatusCode() >= 300) {
                EntityUtils.consume(entity);
                throw new HttpResponseException(statusLine.getStatusCode(), statusLine.getReasonPhrase());
            }
            return entity == null ? null : EntityUtils.toString(entity, defaultEncoding);
        }
    }

    private static HttpClient client;

    private static final int CONNECTION_TIMEOUT = 5000;

    private static PoolingClientConnectionManager connectionManager;
    private static final Logger logger = LoggerFactory.getLogger(HttpClientUtils.class);

    private static final int READ_TIMEOUT = 10000;

    static {
        HttpClientUtils.connectionManager = new PoolingClientConnectionManager();
        HttpClientUtils.connectionManager.setDefaultMaxPerRoute(50);
        HttpClientUtils.connectionManager.setMaxTotal(200);
        HttpParams defaultParams = new SyncBasicHttpParams();

        defaultParams.setParameter(CoreConnectionPNames.SO_TIMEOUT, HttpClientUtils.READ_TIMEOUT);
        defaultParams.setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, HttpClientUtils.CONNECTION_TIMEOUT);

        HttpProtocolParams.setContentCharset(defaultParams, Consts.UTF_8.name());
        HttpProtocolParams.setUseExpectContinue(defaultParams, false);
        DefaultHttpClient.setDefaultHttpParams(defaultParams);
        HttpClientUtils.client = new DefaultHttpClient(HttpClientUtils.connectionManager, defaultParams);
        HttpClientUtils.client = wrapHttpsClient(client);
    }

    private static String getDefaultEncoding(String defaultEncoding) {
        String encoding = StringUtils.isEmpty(defaultEncoding) ? Consts.UTF_8.name() : defaultEncoding;
        return encoding;
    }

    /**
     * ??https
     *
     * @param base
     * @return
     */
    private static HttpClient wrapHttpsClient(HttpClient base) {
        try {
            SSLContext ctx = SSLContext.getInstance("TLS");
            X509TrustManager tm = new X509TrustManager() {
                public X509Certificate[] getAcceptedIssuers() {
                    return null;
                }

                public void checkClientTrusted(X509Certificate[] arg0, String arg1) throws CertificateException {
                }

                public void checkServerTrusted(X509Certificate[] arg0, String arg1) throws CertificateException {
                }
            };
            ctx.init(null, new TrustManager[] { tm }, null);
            SSLSocketFactory ssf = new SSLSocketFactory(ctx, SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
            SchemeRegistry registry = new SchemeRegistry();
            registry.register(new Scheme("https", 443, ssf));
            registry.register(new Scheme("http", 80, PlainSocketFactory.getSocketFactory()));
            ThreadSafeClientConnManager mgr = new ThreadSafeClientConnManager(registry);
            return new DefaultHttpClient(mgr, base.getParams());
        } catch (Exception ex) {
            ex.printStackTrace();
            return null;
        }
    }

    /**
     * url???post?
     *
     * @param url
     * @param formData ?
     * @return
     */
    public static String post(String url, Map<String, String> formData) {
        return HttpClientUtils.post(url, formData, null);
    }

    /**
     * url???post??
     *
     * @param url
     * @param formData
     * @param retryTimes
     * @return
     */
    public static String timeoutRetryPost(String url, Map<String, String> formData, int retryTimes) {
        if (retryTimes < 0) {
            throw new IllegalArgumentException("retry times must great than or equal 0");
        }
        SocketTimeoutException ste = null;
        for (int i = 0; i <= retryTimes; i++) {
            try {
                String result = postWithTimeoutException(url, formData, null);
                if (i > 0) {
                    logger.warn("post [%s] retry %d times", url, i);
                }
                return result;
            } catch (SocketTimeoutException e) {
                ste = e;
            }
        }
        logger.error("post [%s] timeout, retry %d times", url, retryTimes, ste);
        return null;
    }

    /**
     * url???post?
     *
     * @param url
     * @param formData        ?
     * @param defaultEncoding ? form encode ?? contentType ?
     * @return
     */
    public static String post(String url, Map<String, String> formData, String defaultEncoding) {
        try {
            return postWithTimeoutException(url, formData, defaultEncoding);
        } catch (SocketTimeoutException e) {
            HttpClientUtils.logger.error(String.format("post [%s] timeout", url), e);
            return null;
        }
    }

    private static String postWithTimeoutException(String url, Map<String, String> formData, String defaultEncoding)
            throws SocketTimeoutException {
        defaultEncoding = HttpClientUtils.getDefaultEncoding(defaultEncoding);

        HttpPost post = new HttpPost(url);
        String content = null;
        List<NameValuePair> nameValues = new ArrayList<NameValuePair>();
        if (formData != null && !formData.isEmpty()) {
            for (Entry<String, String> entry : formData.entrySet()) {
                nameValues.add(new BasicNameValuePair(entry.getKey(), entry.getValue()));
            }
        }
        try {
            UrlEncodedFormEntity formEntity = new UrlEncodedFormEntity(nameValues, defaultEncoding);
            post.setEntity(formEntity);
            content = HttpClientUtils.client.execute(post, new CharsetableResponseHandler(defaultEncoding));
        } catch (UnsupportedEncodingException e) {
            throw new RuntimeException("unsupported Encoding " + defaultEncoding, e);
        } catch (SocketTimeoutException e) {
            throw e;
        } catch (Exception e) {
            HttpClientUtils.logger.error(String.format("post [%s] happens error ", url), e);
        }
        return content;
    }

    /**
     * url??Jsonpost?
     *
     * @param url
     * @param jsonData
     * @return
     */
    public static String post(String url, String jsonData) {
        return HttpClientUtils.post(url, jsonData, null);
    }

    /**
     * url??Jsonpost?
     *
     * @param url
     * @param jsonData
     * @return
     */
    public static String post(String url, String jsonData, String defaultEncoding) {
        defaultEncoding = HttpClientUtils.getDefaultEncoding(defaultEncoding);
        String content = null;
        try {
            HttpPost post = new HttpPost(url);
            StringEntity entity = new StringEntity(jsonData, defaultEncoding);
            entity.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE, "application/json"));
            post.setEntity(entity);
            HttpResponse response = HttpClientUtils.client.execute(post);
            content = EntityUtils.toString(response.getEntity());
        } catch (Exception e) {
            HttpClientUtils.logger.error(String.format("post [%s] happens error ", url), e);
        }
        return content;
    }

    /**
     * 
     *
     * @param url
     * @param key
     * @param files
     * @return
     */
    public static String postFile(String url, String key, List<File> files) {
        HttpPost post = new HttpPost(url);
        String content = null;

        MultipartEntity multipartEntity = new MultipartEntity();
        for (File file : files) {
            multipartEntity.addPart(key, new FileBody(file));
        }
        try {
            post.setEntity(multipartEntity);
            HttpResponse response = HttpClientUtils.client.execute(post);
            content = EntityUtils.toString(response.getEntity());
        } catch (Exception e) {
            HttpClientUtils.logger.error(String.format("post [%s] happens error ", url), e);
        }

        return content;
    }

    /**
     * url?
     *
     * @param url
     * @return
     */
    public static String request(String url) {
        return HttpClientUtils.request(url, null, null);
    }

    /**
     * url?
     *
     * @param url
     * @param readTimeout
     * @return
     */
    public static String request(String url, Integer readTimeout) {
        return HttpClientUtils.request(url, null, readTimeout);
    }

    /**
     * url?
     *
     * @param url
     * @param defaultEncoding  contentType ??
     * @param readTimeout:    socket timeout
     * @return
     */
    public static String request(String url, String defaultEncoding, Integer readTimeout) {
        defaultEncoding = HttpClientUtils.getDefaultEncoding(defaultEncoding);

        HttpGet get = new HttpGet(url);
        if (readTimeout != null && readTimeout > 0) {
            get.getParams().setParameter(CoreConnectionPNames.SO_TIMEOUT, readTimeout);
        }
        String content = null;
        try {
            content = HttpClientUtils.client.execute(get, new CharsetableResponseHandler(defaultEncoding));
        } catch (Exception e) {
            HttpClientUtils.logger.error(String.format("request [%s] happens error ", url), e);
        }
        return content;
    }

    /**
     * url?
     *
     * @param str    ??
     * @param encode ? GBK, UTF-8 
     * @return ? null str  null
     */
    public static String urlDecode(String str, String encode) {
        if (StringUtils.isEmpty(str)) {
            return null;
        }
        String result = null;
        try {
            result = URLDecoder.decode(str, encode);
        } catch (UnsupportedEncodingException e) {
            HttpClientUtils.logger.error("urldecode error for {} with encode {}", str, encode);
        }
        return result;
    }

    /**
     * url?
     *
     * @param str    ??
     * @param encode ? GBK, UTF-8 
     * @return ? null str  null
     */
    public static String urlEncode(String str, String encode) {
        if (StringUtils.isEmpty(str)) {
            return null;
        }
        String result = null;
        try {
            result = URLEncoder.encode(str, encode);
        } catch (UnsupportedEncodingException e) {
            HttpClientUtils.logger.error("urlencode error for {} with encode {}", str, encode);
        }
        return result;
    }

    /**
     * post???
     *
     * @param url               url
     * @param fileList 
     * @return
     */
    public static String postFile(String url, List<File> fileList) {
        HttpPost httppost = new HttpPost(url);
        String content = null;

        //1.?
        MultipartEntity reqEntity = new MultipartEntity(null, null, Consts.UTF_8);
        for (File tempFile : fileList) {
            reqEntity.addPart(tempFile.getName(), new FileBody(tempFile));
        }
        httppost.setEntity(reqEntity);
        try {
            HttpResponse response = HttpClientUtils.client.execute(httppost);
            content = EntityUtils.toString(response.getEntity());
        } catch (IOException e) {
            HttpClientUtils.logger.error("postFileForResponse error " + e);
        }
        return content;
    }

    /**
     * urlpost?
     *
     * @param url      url
     * @param formData ?
     * @return
     */
    public static HttpResponse postMapForResponse(String url, Map<String, Object> formData) {
        String encoding = HttpClientUtils.getDefaultEncoding(null);

        HttpPost post = new HttpPost(url);
        String param = JsonUtils.marshalToString(formData);
        List<NameValuePair> nameValues = new ArrayList<NameValuePair>();
        nameValues.add(new BasicNameValuePair("param", param));
        try {
            UrlEncodedFormEntity formEntity = new UrlEncodedFormEntity(nameValues, encoding);
            post.setEntity(formEntity);
            return HttpClientUtils.client.execute(post);
        } catch (UnsupportedEncodingException e) {
            throw new RuntimeException("unsupported Encoding " + encoding, e);
        } catch (SocketTimeoutException e) {
            logger.error("error when postForResponse " + e);
        } catch (Exception e) {
            HttpClientUtils.logger.error(String.format("post [%s] happens error ", url), e);
        }
        return null;
    }

    private HttpClientUtils() {
    }
}