com.ms.commons.utilities.HttpClientUtils.java Source code

Java tutorial

Introduction

Here is the source code for com.ms.commons.utilities.HttpClientUtils.java

Source

/*
 * Copyright 2011-2016 ZXC.com All right reserved. This software is the confidential and proprietary information of
 * ZXC.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 ZXC.com.
 */
package com.ms.commons.utilities;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;

import org.apache.commons.httpclient.HostConfiguration;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpMethod;
import org.apache.commons.httpclient.HttpMethodBase;
import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.httpclient.MultiThreadedHttpConnectionManager;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.params.HttpConnectionManagerParams;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;

import com.ms.commons.log.LoggerFactoryWrapper;

/**
 * @author zxc Apr 12, 2013 1:36:20 PM
 */
public class HttpClientUtils {

    private static final Logger logger = LoggerFactoryWrapper.getLogger(HttpClientUtils.class);
    private static MultiThreadedHttpConnectionManager connectionManager = null;
    private static HttpClient client = null;

    public static String getResponseBodyAsString(String url) {
        HttpMethod method = new GetMethod(url);
        return getResponseBodyAsString(method);
    }

    public static String getResponseBodyAsString(HttpMethod method) {
        return getResponseBodyAsString(method, null, null, null, null);
    }

    public static String getResponseBodyAsString(HttpMethod method, Integer tryTimes, String responseCharSet,
            Integer maximumResponseByteSize, Integer soTimeoutMill) {
        init();
        if (tryTimes == null) {
            tryTimes = 1;
        }
        if (StringUtils.isBlank(responseCharSet)) {
            responseCharSet = "utf-8";
        }
        if (maximumResponseByteSize == null) {
            maximumResponseByteSize = 50 * 1024 * 1024;
        }
        if (soTimeoutMill == null) {
            soTimeoutMill = 20000;
        }
        method.getParams().setSoTimeout(soTimeoutMill);
        InputStream httpInputStream = null;
        for (int i = 0; i < tryTimes; i++) {
            try {
                int responseCode = client.executeMethod(method);
                if (responseCode == HttpStatus.SC_OK || responseCode == HttpStatus.SC_MOVED_PERMANENTLY
                        || responseCode == HttpStatus.SC_MOVED_TEMPORARILY) {
                    if (method instanceof HttpMethodBase) {
                        responseCharSet = ((HttpMethodBase) method).getResponseCharSet();
                    }
                    int read = 0;
                    byte[] cbuf = new byte[4096];
                    ByteArrayOutputStream baos = new ByteArrayOutputStream();
                    httpInputStream = method.getResponseBodyAsStream();
                    while ((read = httpInputStream.read(cbuf)) >= 0) {
                        baos.write(cbuf, 0, read);
                        if (baos.size() >= maximumResponseByteSize) {
                            break;
                        }
                    }
                    String content = baos.toString(responseCharSet);
                    return content;
                }
                logger.error(String.format(
                        "getResponseBodyAsString failed, responseCode: %s, should be 200, 301, 302", responseCode));
                return "";
            } catch (Exception e) {
                logger.error("getResponseBodyAsString failed", e);
            } finally {
                IOUtils.closeQuietly(httpInputStream);
                method.releaseConnection();
            }
        }
        return "";
    }

    /**
     * <pre>
     * 
     * 
     * try {
     *      inputStream = getResponseBodyAsStream(method);
     * } finally {
     *      IOUtils.closeQuietly(inputStream);
     *      method.releaseConnection();
     * }
     * 
     * </pre>
     * 
     * @param method
     * @return
     */
    public static InputStream getResponseBodyAsStream(HttpMethod method) {
        return getResponseBodyAsStream(method, null, null);
    }

    /**
     * byte[]?? uri ???getResponseBodyAsStream
     * 
     * @param method
     * @return
     */
    public static byte[] getResponseBodyAsByte(String uri) {
        return getResponseBodyAsByte(new GetMethod(uri));
    }

    /**
     * byte[]??method????getResponseBodyAsStream
     * 
     * @param method
     * @return
     */
    public static byte[] getResponseBodyAsByte(HttpMethod method) {
        init();
        InputStream inputStream = null;
        try {
            inputStream = getResponseBodyAsStream(method, null, null);
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            IOUtils.copy(inputStream, baos);
            return baos.toByteArray();
        } catch (IOException e) {
            logger.error(e.getMessage(), e);
        } finally {
            IOUtils.closeQuietly(inputStream);
            method.releaseConnection();
        }
        return new byte[] {};
    }

    /**
     * <pre>
     * 
     * 
     * try {
     *      inputStream = getResponseBodyAsStream(method, tryTimes, soTimeoutMill);
     * } finally {
     *      IOUtils.closeQuietly(inputStream);
     *      method.releaseConnection();
     * }
     * 
     * @param method
     * @param tryTimes
     * @param soTimeoutMill
     * @return
     */
    public static InputStream getResponseBodyAsStream(HttpMethod method, Integer tryTimes, Integer soTimeoutMill) {
        init();
        if (tryTimes == null) {
            tryTimes = 1;
        }
        if (soTimeoutMill == null) {
            soTimeoutMill = 20000;
        }
        method.getParams().setSoTimeout(soTimeoutMill);
        for (int i = 0; i < tryTimes; i++) {
            try {
                int responseCode = client.executeMethod(method);
                if (responseCode == HttpStatus.SC_OK || responseCode == HttpStatus.SC_MOVED_PERMANENTLY
                        || responseCode == HttpStatus.SC_MOVED_TEMPORARILY) {
                    return method.getResponseBodyAsStream();
                }
                logger.error(String.format(
                        "getResponseBodyAsString failed, responseCode: %s, should be 200, 301, 302", responseCode));
            } catch (Exception e) {
                logger.error("getResponseBodyAsString failed", e);
            } finally {
                // method releaseConnection  ResponseStream ResponseStream
                // ?finally { method.releaseConnection }
                // method.releaseConnection();
            }
        }
        return null;
    }

    public static synchronized void init() {
        if (connectionManager != null) {
            return;
        }
        connectionManager = new MultiThreadedHttpConnectionManager();
        HttpConnectionManagerParams params = new HttpConnectionManagerParams();
        params.setConnectionTimeout(10000);
        params.setSoTimeout(20000);
        params.setMaxConnectionsPerHost(HostConfiguration.ANY_HOST_CONFIGURATION, 1000);
        params.setMaxTotalConnections(10000);
        connectionManager.setParams(params);
        client = new HttpClient(connectionManager);
        client.getParams().setParameter("http.protocol.max-redirects", 3);
    }

}