costumetrade.common.util.HttpClientUtils.java Source code

Java tutorial

Introduction

Here is the source code for costumetrade.common.util.HttpClientUtils.java

Source

/**
 * Copyright (C) 2014-2016, hrfax and/or its affiliates. All rights reserved.
 * hrfax PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 *
 */
package costumetrade.common.util;

import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.commons.io.IOUtils;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.cookie.params.CookieSpecPNames;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * @author dante
 * @Date 20161114
 */
public class HttpClientUtils {

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

    public static String doPost(String url, HttpEntity httpEntity) {

        logger.info("?{}....", url);

        try (CloseableHttpClient httpclient = HttpClients.custom().build();) {
            HttpPost request = new HttpPost(url);
            request.setEntity(httpEntity);
            try (CloseableHttpResponse response = httpclient.execute(request);) {
                int statusCode = response.getStatusLine().getStatusCode();
                logger.info("?{}???{}", url, statusCode);
                if (HttpStatus.SC_OK == statusCode) {
                    return IOUtils.toString(response.getEntity().getContent(), StandardCharsets.UTF_8.name());
                }
                throw new RuntimeException(String.format("?%s???%s", url, statusCode));
            }
        } catch (Exception e) {
            throw new RuntimeException(String.format("%s", url), e);
        }
    }

    /**
     * @param url
     * @param ?
     * @return
     */
    public static String doPost(String url, Map<String, Object> params) {
        List<BasicNameValuePair> nvps = new ArrayList<>(params.size());
        for (String key : params.keySet()) {
            Object value = params.get(key);
            if (value != null) {
                nvps.add(new BasicNameValuePair(key, value.toString()));
            }
        }
        return doPost(url, new UrlEncodedFormEntity(nvps, StandardCharsets.UTF_8));
    }

    public static InputStream getInputStream(String url) {

        try (CloseableHttpClient httpclient = HttpClients.custom().build();) {
            HttpGet request = new HttpGet(url);
            try (CloseableHttpResponse response = httpclient.execute(request);) {
                int statusCode = response.getStatusLine().getStatusCode();
                logger.info("?{}???{}", url, statusCode);
                if (HttpStatus.SC_OK == statusCode) {
                    return response.getEntity().getContent();
                }
                throw new RuntimeException(String.format("?%s???%s", url, statusCode));
            }
        } catch (IOException e) {
            throw new RuntimeException(String.format("%s", url));
        }
    }

    public static String get(String url) {

        try (InputStream inputStream = getInputStream(url);) {
            return IOUtils.toString(inputStream, StandardCharsets.UTF_8.name());
        } catch (IOException e) {
            throw new RuntimeException("?,url" + url);
        }

    }

    public static String get(String url, String encoding) throws ClientProtocolException, IOException {
        DefaultHttpClient httpclient = new DefaultHttpClient();
        try {
            httpclient.getParams().setBooleanParameter(CookieSpecPNames.SINGLE_COOKIE_HEADER, true);
            //log.info("GET "+url);
            HttpGet httpget = new HttpGet(url);
            httpget.addHeader("User-Agent",
                    "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; .NET CLR 2.0.50727; .NET CLR 3.0.04506.648; .NET CLR 3.5.21022)");
            HttpResponse response = httpclient.execute(httpget);
            //log.info(response.getStatusLine().getStatusCode());
            HttpEntity entity = response.getEntity();
            return EntityUtils.toString(entity, encoding);
        } finally {
            if (httpclient != null) {
                httpclient.getConnectionManager().shutdown();
            }
        }
    }

}