HttpClientUtil工具类

HttpClientUtil工具类,用于进行Post、Get请求,HttpClient 是Apache Jakarta Common 下的子项目,可以用来提供高效的、最新的、功能丰富的支持 HTTP 协议的客户端编程工具包,并且它支持 HTTP 协议最新的版本和建议。

package com.ly.spider.http;

import com.ly.spider.proxy.Proxy;
import com.ly.spider.proxy.ProxyContainer;
import org.apache.http.HttpHost;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.config.CookieSpecs;
import org.apache.http.client.config.RequestConfig;
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.client.methods.HttpRequestBase;
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
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.entity.mime.content.StringBody;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.ssl.SSLContexts;
import org.apache.http.util.EntityUtils;

import javax.net.ssl.SSLContext;
import java.io.File;
import java.io.IOException;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.commons.lang3.StringUtils;


/**
 * httpclient 简单工具类
 *
 * @author eric
 * @version $Id: HttpClientUtil.java, v 0.1 2016年5月10日 上午9:59:38 tkj09938 Exp $
 */
public class HttpClientUtil {
    /**
     * 处理get请求
     *
     * @param httpClient
     * @param url
     * @return
     * @throws IOException
     */
    public static HttpContext doGet(HttpClient httpClient, String url) throws IOException {
        return doGet(httpClient, url, new HashMap<String, Object>());
    }

    /**
     * 处理get请求
     *
     * @param client
     * @param url
     * @param params
     * @return
     * @throws IOException
     */
    public static HttpContext doGet(HttpClient client, String url, Map<String, Object> params) throws IOException {

        return doGet(client, url, params, new HashMap<String, String>());

    }

    /**
     * 处理get请求
     *
     * @param client
     * @param url
     * @param params
     * @param headers
     * @return
     * @throws IOException
     */
    public static HttpContext doGet(HttpClient client, String url, Map<String, Object> params,
                                    Map<String, String> headers) throws IOException {
        StringBuffer param = new StringBuffer();
        if (params != null && !params.isEmpty()) {
            int i = 0;
            for (String key : params.keySet()) {
                if (i++ == 0) {
                    param.append("?");
                } else {
                    param.append("&");
                }
                param.append(key).append("=").append(params.get(key));
            }
        }
        HttpGet getRequest = new HttpGet(url + param.toString());
        initHttpHeader(getRequest, headers);
        HttpResponse response = client.execute(getRequest);
        return new HttpContext(response, getRequest);
    }

    /**
     * 处理空参post
     *
     * @param client
     * @param url
     * @return
     * @throws IOException
     */
    public static HttpContext doPost(HttpClient client, String url) throws IOException {
        return doPost(client, url, new HashMap<String, Object>());
    }

    /**
     * 处理带有表单参数的post
     *
     * @param client
     * @param url
     * @param params
     * @return
     * @throws IOException
     */
    public static HttpContext doPost(HttpClient client, String url, Map<String, Object> params) throws IOException {
        return doPost(client, url, params, new HashMap<String, String>());
    }

    /**
     * 处理带有表单参数和消息头的post
     *
     * @param client
     * @param url
     * @param params
     * @param headers
     * @return
     * @throws IOException
     */
    public static HttpContext doPost(HttpClient client, String url, Map<String, Object> params,
                                     Map<String, String> headers) throws IOException {
        HttpPost postRequest = new HttpPost(url);
        initHttpHeader(postRequest, headers);
        // 构造表单参数
        if (params != null && !params.isEmpty()) {
            List<NameValuePair> pairList = new ArrayList<NameValuePair>(params.size());
            for (String key : params.keySet()) {
                if (params.get(key) != null) {
                    NameValuePair pair = new BasicNameValuePair(key, params.get(key).toString());
                    pairList.add(pair);
                }
            }
            postRequest.setEntity(new UrlEncodedFormEntity(pairList, Charset.forName("UTF-8")));
        }
        HttpResponse response = client.execute(postRequest);
        return new HttpContext(response, postRequest);
    }

    /**
     * 处理mulitipart表单
     *
     * @param client
     * @param url
     * @param params
     * @param headers
     * @return
     * @throws IOException
     */
    @SuppressWarnings("deprecation")
    public static HttpContext doPostMultipartData(HttpClient client, String url, Map<String, Object> params,
                                                  Map<String, String> headers) throws IOException {
        HttpPost postRequest = new HttpPost(url);
        initHttpHeader(postRequest, headers);
        MultipartEntity multipartEntity = new MultipartEntity();
        for (String key : params.keySet()) {
            if (params.get(key) instanceof String) {
                multipartEntity.addPart(key, new StringBody((String) params.get(key), Charset.forName("UTF-8")));
            } else if (params.get(key) instanceof File) {
                multipartEntity.addPart(key, new FileBody((File) params.get(key)));
            }
        }
        postRequest.setEntity(multipartEntity);
        HttpResponse response = client.execute(postRequest);
        return new HttpContext(response, postRequest);
    }

    /**
     * 处理其他类型post
     *
     * @param client
     * @param url
     * @param param
     * @return
     * @throws Exception
     */
    public static HttpContext doPost(HttpClient client, String url, String param) throws Exception {
        return doPost(client, url, param, new HashMap<String, String>());
    }

    /**
     * 处理其他类型post
     *
     * @param client
     * @param url
     * @param param
     * @param headers
     * @return
     * @throws Exception
     */
    public static HttpContext doPost(HttpClient client, String url, String param, Map<String, String> headers)
            throws Exception {
        HttpPost postRequest = new HttpPost(url);
        initHttpHeader(postRequest, headers);
        StringEntity stringEn = new StringEntity(param, "UTF-8");
        postRequest.setEntity(stringEn);
        HttpResponse response = client.execute(postRequest);
        return new HttpContext(response, postRequest);
    }

    /**
     * 初始化http消息头
     *
     * @param client
     * @param headers
     */
    private static void initHttpHeader(HttpRequestBase request, Map<String, String> headers) {
        if (headers != null && !headers.isEmpty()) {
            for (String key : headers.keySet()) {
                if (!StringUtils.isEmpty(headers.get(key))) {
                    request.addHeader(key, headers.get(key));
                }
            }
        }

    }

    /**
     * 解析http响应状态码
     *
     * @param httpResponse
     * @return
     */
    public static int getResponseCode(HttpResponse httpResponse) {
        if (httpResponse != null) {
            return httpResponse.getStatusLine().getStatusCode();
        }
        return 0;
    }

    /**
     * 使用默认的编码解析http响应实体
     *
     * @param httpResponse
     * @return
     */
    public static String getResponseContent(HttpContext httpContext) throws Exception {
        return getResponseContent(httpContext, "UTF-8");
    }

    /**
     * 将http 响应实体解析为字节数组
     *
     * @param httpResponse
     * @return
     * @throws Exception
     */
    public static byte[] getResponseContentBytes(HttpContext httpContext) throws Exception {
        byte[] result = null;
        try {
            if (httpContext.getResponse() != null) {
                result = EntityUtils.toByteArray(httpContext.getResponse().getEntity());
            }
        } catch (Exception e) {
            throw e;
        } finally {
            EntityUtils.consume(httpContext.getResponse().getEntity());
            httpContext.getRequest().abort();
        }
        return result;
    }

    /**
     * 以特定的字符集编码解析http响应实体
     *
     * @param httpResponse
     * @param charset
     * @return
     * @throws Exception
     */
    public static String getResponseContent(HttpContext httpContext, String charset) throws Exception {
        String result = "";
        try {
            if (httpContext.getResponse() != null) {
                result = EntityUtils.toString(httpContext.getResponse().getEntity(), charset);
            }
        } catch (Exception e) {
            throw e;
        } finally {
            EntityUtils.consume(httpContext.getResponse().getEntity());
            httpContext.getRequest().abort();
        }
        return result;
    }

    /**
     * 获取默认的httpClient
     *
     * @return
     */
    public static CloseableHttpClient getDefaultHttpClient() {
        RequestConfig config = RequestConfig.custom().setCookieSpec(CookieSpecs.STANDARD_STRICT)
                .setConnectTimeout(10000).setSocketTimeout(30000).build();
        return HttpClients.custom().setDefaultRequestConfig(config).build();
    }

    /**
     * 获取使用云代理的httpClient
     *
     * @return
     */
    public static CloseableHttpClient getHttpClientWithProxy() {
        RequestConfig config = RequestConfig.custom().setCookieSpec(CookieSpecs.STANDARD_STRICT)
                .setConnectTimeout(10000).setSocketTimeout(30000).setProxy(getProxyHost()).build();
        return HttpClients.custom().setDefaultRequestConfig(config).build();

    }

    /**
     * 代理主机(公共云代理)
     *
     * @return
     */
    private static HttpHost getProxyHost() {
        Proxy proxy = ProxyContainer.getInstance().getProxy();
        return new HttpHost(proxy.getHost(), proxy.getPort(), "http");
    }

    /**
     * 获取使用认证代理(公共拨号代理)的httpClient;
     *
     * @return
     */
    /*public static CloseableHttpClient getHttpClientWithAuthProxy() {
        RequestConfig config = RequestConfig.custom().setCookieSpec(CookieSpecs.STANDARD_STRICT)
                .setConnectTimeout(10000).setSocketTimeout(30000).setProxy(getProxyHost()).build();
        CredentialsProvider credsProvider = new BasicCredentialsProvider();// 创建认证
        credsProvider.setCredentials(
                new AuthScope(AdslProxyHelper.getProxy().getHost(), AdslProxyHelper.getProxy().getPort()),
                new UsernamePasswordCredentials(AdslProxyHelper.getProxy().getUsername(),
                        AdslProxyHelper.getProxy().getPassword()));// 用户名和密码
        return HttpClients.custom().setDefaultCredentialsProvider(credsProvider).setDefaultRequestConfig(config)
                .build();
    }*/

    /**
     * 校验http响应是否为200
     *
     * @param response
     * @return
     * @throws Exception
     */
    public static boolean checkResponseCodeOk(HttpResponse response) throws Exception {
        return HttpStatus.SC_OK == getResponseCode(response);
    }

    /**
     * 校验http响应是否为302
     *
     * @param response
     * @return
     * @throws Exception
     */
    public static boolean checkResponseRedirect(HttpResponse response) throws Exception {
        return HttpStatus.SC_MOVED_TEMPORARILY == getResponseCode(response);
    }
    /**
     * 获取重定向地址
     * @param response
     * @return
     * @throws Exception
     */
    public static String getRedirectUrl(HttpContext context) throws Exception{
        String redirectUrl = context.getResponse().getFirstHeader("Location").getValue();
        abortHttpContex(context);
        return redirectUrl;

    }

    /**
     * 释放掉链接
     *
     * @param context
     */
    public static void abortHttpContex(HttpContext context) {
        context.getRequest().abort();
    }

    /**
     * 处理https的get请求
     *
     * @param url
     * @return
     * @throws IOException
     * @throws ClientProtocolException
     */
    public static HttpContext httpsGet(String url) throws ClientProtocolException, IOException {
        HttpClient client = createSSLInsecureClient();
        return doGet(client, url);
    }

    /**
     * 处理https的get请求
     *
     * @param url
     * @return
     * @throws IOException
     * @throws ClientProtocolException
     */
    public static HttpContext httpsGet(HttpClient client, String url) throws ClientProtocolException, IOException {
        return doGet(client, url);
    }

    /**
     * 处理https的post请求
     *
     * @param url
     * @param params
     * @param headers
     * @return
     * @throws IOException
     */
    public static HttpContext httpsPost(String url, Map<String, Object> params, Map<String, String> headers)
            throws IOException {
        HttpClient client = createSSLInsecureClient();
        return doPost(client, url, params, headers);
    }

    /**
     * 处理https的post请求
     *
     * @param client
     * @param url
     * @param params
     * @param headers
     * @return
     * @throws IOException
     */
    public static HttpContext httpsPost(HttpClient client, String url, Map<String, Object> params,
                                        Map<String, String> headers) throws IOException {
        return doPost(client, url, params, headers);
    }

    /**
     * 创建 SSL连接
     *
     * @return
     */
    public static CloseableHttpClient createSSLInsecureClient() {
        SSLContext sslContext = SSLContexts.createSystemDefault();
        SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(sslContext);
        return HttpClients.custom().setSSLSocketFactory(sslsf).build();
    }

}

  转载请注明: Hi 高虎 HttpClientUtil工具类

  目录