常用工具类(二):HttpUtil 发送HTTP请求

常用工具类(二):HttpUtil 发送HTTP请求

public class HttpUtil {
    private static Logger logger = LoggerFactory.getLogger(HttpUtil.class);

    /**
     *
     * @param url 需要请求的地址
     * @param jsonParam 请求地址需要携带的参数
     * @return
     */
    public static String httpPost(String url, String jsonParam) {
        //RequestConfig 用于配置网络环境
        RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(50000).setConnectTimeout(10000).build();
        return httpPostExecute(url,jsonParam,requestConfig);
    }

    public static String httpPostExecute(String url, String jsonParam, RequestConfig requestConfig){
        HttpClient httpClient = HttpClients.createDefault();
        HttpPost httpPost = new HttpPost(url);
        if (requestConfig != null) httpPost.setConfig(requestConfig);
        if (!StringUtils.isEmpty(jsonParam)){
            //设置消息体
            StringEntity entity = new StringEntity(jsonParam, "utf-8");
            entity.setContentEncoding("utf-8");
            entity.setContentType("application/json");

            httpPost.setHeader("Accept-Encoding","gzip,deflate");
            httpPost.setEntity(entity);
        }

        try {
            HttpResponse result = httpClient.execute(httpPost);
            //encode编码  decode解码
            url = URLDecoder.decode(url,"utf-8");
            if (result.getStatusLine().getStatusCode() == 200) {
                String str = "";
                Header contentHeader = result.getFirstHeader("Content-Encoding");
                if (contentHeader != null && contentHeader.getValue().toLowerCase().indexOf("gzip") > -1) {
                    str = EntityUtils.toString(new GzipDecompressingEntity(result.getEntity()),"utf-8");
                    return str;
                } else {
                    str = result.getEntity().toString();
                    return str;
                }
             }
        } catch (IOException e) {
            logger.info(url+"请求失败",e);
        }
        return null;
    }


    public static String httpGet(String url) {
        RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(50000).setConnectionRequestTimeout(10000).build();
        return httpGetExecute(url,requestConfig);
    }

    public static String httpGetExecute(String url, RequestConfig requestConfig){
        HttpClient httpClient = HttpClients.createDefault();
        HttpGet httpGet = new HttpGet(url);
        if (requestConfig != null)  httpGet.setConfig(requestConfig);
        httpGet.setHeader("Accept-Encoding", "gzip,default");

        try {
            HttpResponse result = httpClient.execute(httpGet);
            String str = null;
            if (result.getStatusLine().getStatusCode() == 200) {
                Header contentHeader = result.getFirstHeader("Content-Encoding");
                if (contentHeader != null && contentHeader.getValue().toLowerCase().indexOf("gzip") > -1) {
                    str = EntityUtils.toString(new GzipDecompressingEntity(result.getEntity()));
                    return str;
                } else {
                    str = EntityUtils.toString(result.getEntity(),"utf-8");
                    return str;
                }
            }
        } catch (IOException e) {
            logger.info(url+"请求失败",e);
        }
        return null;
    }
}

你可能感兴趣的:(Utils,java)