|  | @@ -0,0 +1,478 @@
 | 
	
		
			
				|  |  | +/**
 | 
	
		
			
				|  |  | + * @Title: EasyHttpUtils.java
 | 
	
		
			
				|  |  | + * @Package com.elite.common.utils.httpclient
 | 
	
		
			
				|  |  | + * @Description: TODO(用一句话描述该文件做什么)
 | 
	
		
			
				|  |  | + * @author admin
 | 
	
		
			
				|  |  | + * @date 2016年5月23日 下午5:30:48
 | 
	
		
			
				|  |  | + * @version V1.0
 | 
	
		
			
				|  |  | + */
 | 
	
		
			
				|  |  | +package com.hw.util;
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +import com.alibaba.fastjson.JSONObject;
 | 
	
		
			
				|  |  | +import lombok.extern.slf4j.Slf4j;
 | 
	
		
			
				|  |  | +import org.apache.http.*;
 | 
	
		
			
				|  |  | +import org.apache.http.client.ClientProtocolException;
 | 
	
		
			
				|  |  | +import org.apache.http.client.HttpRequestRetryHandler;
 | 
	
		
			
				|  |  | +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.CloseableHttpResponse;
 | 
	
		
			
				|  |  | +import org.apache.http.client.methods.HttpGet;
 | 
	
		
			
				|  |  | +import org.apache.http.client.methods.HttpPost;
 | 
	
		
			
				|  |  | +import org.apache.http.client.protocol.HttpClientContext;
 | 
	
		
			
				|  |  | +import org.apache.http.conn.ConnectTimeoutException;
 | 
	
		
			
				|  |  | +import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
 | 
	
		
			
				|  |  | +import org.apache.http.conn.ssl.SSLContexts;
 | 
	
		
			
				|  |  | +import org.apache.http.conn.ssl.TrustSelfSignedStrategy;
 | 
	
		
			
				|  |  | +import org.apache.http.entity.StringEntity;
 | 
	
		
			
				|  |  | +import org.apache.http.impl.client.CloseableHttpClient;
 | 
	
		
			
				|  |  | +import org.apache.http.impl.client.HttpClients;
 | 
	
		
			
				|  |  | +import org.apache.http.impl.client.LaxRedirectStrategy;
 | 
	
		
			
				|  |  | +import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
 | 
	
		
			
				|  |  | +import org.apache.http.message.BasicNameValuePair;
 | 
	
		
			
				|  |  | +import org.apache.http.protocol.HttpContext;
 | 
	
		
			
				|  |  | +import org.apache.http.util.EntityUtils;
 | 
	
		
			
				|  |  | +import org.slf4j.Logger;
 | 
	
		
			
				|  |  | +import org.slf4j.LoggerFactory;
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +import javax.net.ssl.SSLContext;
 | 
	
		
			
				|  |  | +import javax.net.ssl.SSLException;
 | 
	
		
			
				|  |  | +import java.io.*;
 | 
	
		
			
				|  |  | +import java.net.HttpURLConnection;
 | 
	
		
			
				|  |  | +import java.net.UnknownHostException;
 | 
	
		
			
				|  |  | +import java.nio.charset.Charset;
 | 
	
		
			
				|  |  | +import java.security.KeyManagementException;
 | 
	
		
			
				|  |  | +import java.security.KeyStore;
 | 
	
		
			
				|  |  | +import java.security.KeyStoreException;
 | 
	
		
			
				|  |  | +import java.security.NoSuchAlgorithmException;
 | 
	
		
			
				|  |  | +import java.security.cert.CertificateException;
 | 
	
		
			
				|  |  | +import java.util.ArrayList;
 | 
	
		
			
				|  |  | +import java.util.List;
 | 
	
		
			
				|  |  | +import java.util.Map;
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +/**
 | 
	
		
			
				|  |  | + *
 | 
	
		
			
				|  |  | + * @version 1.0
 | 
	
		
			
				|  |  | + */
 | 
	
		
			
				|  |  | +@Slf4j
 | 
	
		
			
				|  |  | +public class EasyHttpUtils {
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +    private static final Logger LOGG = LoggerFactory.getLogger(EasyHttpUtils.class);
 | 
	
		
			
				|  |  | +    //连接池最大数量
 | 
	
		
			
				|  |  | +    private static final int HTTPCLIENT_CONNECTION_COUNT = 200;
 | 
	
		
			
				|  |  | +    //单个路由最大连接数量
 | 
	
		
			
				|  |  | +    private static final int HTTPCLIENT_MAXPERROUTE_COUNT = 2;
 | 
	
		
			
				|  |  | +    //连接超时
 | 
	
		
			
				|  |  | +    private static final int HTTPCLIENT_CONNECT_TIMEOUT = 600000;
 | 
	
		
			
				|  |  | +    //socket超时
 | 
	
		
			
				|  |  | +    private static final int HTTPCLIENT_SOCKET_TIMEOUT = 600000;
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +    // 创建httpclient连接池
 | 
	
		
			
				|  |  | +    private PoolingHttpClientConnectionManager httpClientConnectionManager = null;
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +    private static final EasyHttpUtils EASY_HTTP_UTILS = new EasyHttpUtils();
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +    public static EasyHttpUtils LoadInstance() {
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +        return EASY_HTTP_UTILS;
 | 
	
		
			
				|  |  | +    }
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +    private EasyHttpUtils() {
 | 
	
		
			
				|  |  | +        initHttpClient();
 | 
	
		
			
				|  |  | +    }
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +    /**
 | 
	
		
			
				|  |  | +     * @Title: initHttpUtils
 | 
	
		
			
				|  |  | +     * @Description: TODO(这里用一句话描述这个方法的作用)
 | 
	
		
			
				|  |  | +     */
 | 
	
		
			
				|  |  | +    private void initHttpClient() {
 | 
	
		
			
				|  |  | +        //创建httpclient连接池
 | 
	
		
			
				|  |  | +        httpClientConnectionManager = new PoolingHttpClientConnectionManager();
 | 
	
		
			
				|  |  | +        //设置连接池最大数量  
 | 
	
		
			
				|  |  | +        httpClientConnectionManager.setMaxTotal(HTTPCLIENT_CONNECTION_COUNT);
 | 
	
		
			
				|  |  | +        //设置单个路由最大连接数量  
 | 
	
		
			
				|  |  | +        httpClientConnectionManager.setDefaultMaxPerRoute(HTTPCLIENT_MAXPERROUTE_COUNT);
 | 
	
		
			
				|  |  | +    }
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +    //请求重试机制
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +    HttpRequestRetryHandler myRetryHandler = new HttpRequestRetryHandler() {
 | 
	
		
			
				|  |  | +        @Override
 | 
	
		
			
				|  |  | +        public boolean retryRequest(IOException exception, int executionCount, HttpContext context) {
 | 
	
		
			
				|  |  | +            if (executionCount >= 3) {
 | 
	
		
			
				|  |  | +                // 超过三次则不再重试请求  
 | 
	
		
			
				|  |  | +                return false;
 | 
	
		
			
				|  |  | +            }
 | 
	
		
			
				|  |  | +            if (exception instanceof InterruptedIOException) {
 | 
	
		
			
				|  |  | +                // Timeout  
 | 
	
		
			
				|  |  | +                return false;
 | 
	
		
			
				|  |  | +            }
 | 
	
		
			
				|  |  | +            if (exception instanceof UnknownHostException) {
 | 
	
		
			
				|  |  | +                // Unknown host  
 | 
	
		
			
				|  |  | +                return false;
 | 
	
		
			
				|  |  | +            }
 | 
	
		
			
				|  |  | +            if (exception instanceof ConnectTimeoutException) {
 | 
	
		
			
				|  |  | +                // Connection refused  
 | 
	
		
			
				|  |  | +                return false;
 | 
	
		
			
				|  |  | +            }
 | 
	
		
			
				|  |  | +            if (exception instanceof SSLException) {
 | 
	
		
			
				|  |  | +                // SSL handshake exception  
 | 
	
		
			
				|  |  | +                return false;
 | 
	
		
			
				|  |  | +            }
 | 
	
		
			
				|  |  | +            HttpClientContext clientContext = HttpClientContext.adapt(context);
 | 
	
		
			
				|  |  | +            HttpRequest request = clientContext.getRequest();
 | 
	
		
			
				|  |  | +            boolean idempotent = !(request instanceof HttpEntityEnclosingRequest);
 | 
	
		
			
				|  |  | +            if (idempotent) {
 | 
	
		
			
				|  |  | +                // Retry if the request is considered idempotent  
 | 
	
		
			
				|  |  | +                return true;
 | 
	
		
			
				|  |  | +            }
 | 
	
		
			
				|  |  | +            return false;
 | 
	
		
			
				|  |  | +        }
 | 
	
		
			
				|  |  | +    };
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +    public CloseableHttpClient getHttpClient() {
 | 
	
		
			
				|  |  | +        // 创建全局的requestConfig  
 | 
	
		
			
				|  |  | +        RequestConfig requestConfig = RequestConfig.custom()
 | 
	
		
			
				|  |  | +                .setConnectTimeout(HTTPCLIENT_CONNECT_TIMEOUT)
 | 
	
		
			
				|  |  | +                .setSocketTimeout(HTTPCLIENT_SOCKET_TIMEOUT)
 | 
	
		
			
				|  |  | +                .setCookieSpec(CookieSpecs.BEST_MATCH).build();
 | 
	
		
			
				|  |  | +        // 声明重定向策略对象  
 | 
	
		
			
				|  |  | +        LaxRedirectStrategy redirectStrategy = new LaxRedirectStrategy();
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +        CloseableHttpClient httpClient = HttpClients.custom().setConnectionManager(httpClientConnectionManager)
 | 
	
		
			
				|  |  | +                .setDefaultRequestConfig(requestConfig)
 | 
	
		
			
				|  |  | +                .setRedirectStrategy(redirectStrategy)
 | 
	
		
			
				|  |  | +                .setRetryHandler(myRetryHandler)
 | 
	
		
			
				|  |  | +                .build();
 | 
	
		
			
				|  |  | +        return httpClient;
 | 
	
		
			
				|  |  | +    }
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +    /**
 | 
	
		
			
				|  |  | +     * HttpClient连接SSL 
 | 
	
		
			
				|  |  | +     */
 | 
	
		
			
				|  |  | +    public void ssl(String urlStr, String keyPath, String keypass) {
 | 
	
		
			
				|  |  | +        CloseableHttpClient httpclient = null;
 | 
	
		
			
				|  |  | +        try {
 | 
	
		
			
				|  |  | +            RequestConfig requestConfig = RequestConfig.custom()
 | 
	
		
			
				|  |  | +                    .setConnectTimeout(HTTPCLIENT_CONNECT_TIMEOUT)
 | 
	
		
			
				|  |  | +                    .setSocketTimeout(HTTPCLIENT_SOCKET_TIMEOUT)
 | 
	
		
			
				|  |  | +                    .setCookieSpec(CookieSpecs.BEST_MATCH).build();
 | 
	
		
			
				|  |  | +            LaxRedirectStrategy redirectStrategy = new LaxRedirectStrategy();
 | 
	
		
			
				|  |  | +            KeyStore trustStore = KeyStore.getInstance(KeyStore.getDefaultType());
 | 
	
		
			
				|  |  | +            FileInputStream instream = new FileInputStream(new File(keyPath));
 | 
	
		
			
				|  |  | +            try {
 | 
	
		
			
				|  |  | +                // 加载keyStore   
 | 
	
		
			
				|  |  | +                trustStore.load(instream, keypass.toCharArray());
 | 
	
		
			
				|  |  | +            } catch (CertificateException e) {
 | 
	
		
			
				|  |  | +                e.printStackTrace();
 | 
	
		
			
				|  |  | +            } finally {
 | 
	
		
			
				|  |  | +                try {
 | 
	
		
			
				|  |  | +                    instream.close();
 | 
	
		
			
				|  |  | +                } catch (Exception ignore) {
 | 
	
		
			
				|  |  | +                }
 | 
	
		
			
				|  |  | +            }
 | 
	
		
			
				|  |  | +            // 相信自己的CA和所有自签名的证书  
 | 
	
		
			
				|  |  | +            SSLContext sslcontext = SSLContexts.custom().loadTrustMaterial(trustStore, new TrustSelfSignedStrategy()).build();
 | 
	
		
			
				|  |  | +            // 只允许使用TLSv1协议  
 | 
	
		
			
				|  |  | +            SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(sslcontext, new String[]{"TLSv1"}, null,
 | 
	
		
			
				|  |  | +                    SSLConnectionSocketFactory.BROWSER_COMPATIBLE_HOSTNAME_VERIFIER);
 | 
	
		
			
				|  |  | +            httpclient = HttpClients.custom().setConnectionManager(httpClientConnectionManager)
 | 
	
		
			
				|  |  | +                    .setDefaultRequestConfig(requestConfig)
 | 
	
		
			
				|  |  | +                    .setRedirectStrategy(redirectStrategy)
 | 
	
		
			
				|  |  | +                    .setRetryHandler(myRetryHandler).setSSLSocketFactory(sslsf).build();
 | 
	
		
			
				|  |  | +            // 创建http请求(get方式)  
 | 
	
		
			
				|  |  | +            HttpGet httpget = new HttpGet(urlStr);
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +            CloseableHttpResponse response = httpclient.execute(httpget);
 | 
	
		
			
				|  |  | +            try {
 | 
	
		
			
				|  |  | +                HttpEntity entity = response.getEntity();
 | 
	
		
			
				|  |  | +                LOGG.info("----------------------------------------");
 | 
	
		
			
				|  |  | +                LOGG.info(String.valueOf(response.getStatusLine()));
 | 
	
		
			
				|  |  | +                if (entity != null) {
 | 
	
		
			
				|  |  | +                    LOGG.info("Response content length: " + entity.getContentLength());
 | 
	
		
			
				|  |  | +                    LOGG.info(EntityUtils.toString(entity));
 | 
	
		
			
				|  |  | +                    EntityUtils.consume(entity);
 | 
	
		
			
				|  |  | +                }
 | 
	
		
			
				|  |  | +            } finally {
 | 
	
		
			
				|  |  | +                response.close();
 | 
	
		
			
				|  |  | +            }
 | 
	
		
			
				|  |  | +        } catch (ParseException e) {
 | 
	
		
			
				|  |  | +            e.printStackTrace();
 | 
	
		
			
				|  |  | +        } catch (IOException e) {
 | 
	
		
			
				|  |  | +            e.printStackTrace();
 | 
	
		
			
				|  |  | +        } catch (KeyManagementException e) {
 | 
	
		
			
				|  |  | +            e.printStackTrace();
 | 
	
		
			
				|  |  | +        } catch (NoSuchAlgorithmException e) {
 | 
	
		
			
				|  |  | +            e.printStackTrace();
 | 
	
		
			
				|  |  | +        } catch (KeyStoreException e) {
 | 
	
		
			
				|  |  | +            e.printStackTrace();
 | 
	
		
			
				|  |  | +        }
 | 
	
		
			
				|  |  | +    }
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +    /**
 | 
	
		
			
				|  |  | +     * 发送 post请求访问本地应用并根据传递参数不同返回不同结果 
 | 
	
		
			
				|  |  | +     */
 | 
	
		
			
				|  |  | +    public void post(String urlString, Map<String, Object> params) {
 | 
	
		
			
				|  |  | +        // 创建默认的httpClient实例. 
 | 
	
		
			
				|  |  | +        LOGG.info("urlString:=========>" + urlString);
 | 
	
		
			
				|  |  | +        CloseableHttpClient httpclient = this.getHttpClient();
 | 
	
		
			
				|  |  | +        if ("".equals(urlString) || urlString == null) {
 | 
	
		
			
				|  |  | +            return;
 | 
	
		
			
				|  |  | +        }
 | 
	
		
			
				|  |  | +        // 创建httppost    
 | 
	
		
			
				|  |  | +        HttpPost httppost = new HttpPost(urlString);
 | 
	
		
			
				|  |  | +        // 创建参数队列    
 | 
	
		
			
				|  |  | +        List<NameValuePair> formparams = new ArrayList<NameValuePair>();
 | 
	
		
			
				|  |  | +        if (params != null && params.size() > 0) {
 | 
	
		
			
				|  |  | +            for (String key : params.keySet()) {
 | 
	
		
			
				|  |  | +                formparams.add(new BasicNameValuePair(key, (String) params.get(key)));
 | 
	
		
			
				|  |  | +            }
 | 
	
		
			
				|  |  | +        }
 | 
	
		
			
				|  |  | +        UrlEncodedFormEntity uefEntity;
 | 
	
		
			
				|  |  | +        try {
 | 
	
		
			
				|  |  | +            uefEntity = new UrlEncodedFormEntity(formparams, "UTF-8");
 | 
	
		
			
				|  |  | +            httppost.setEntity(uefEntity);
 | 
	
		
			
				|  |  | +            LOGG.info("executing request " + httppost.getURI());
 | 
	
		
			
				|  |  | +            CloseableHttpResponse response = httpclient.execute(httppost);
 | 
	
		
			
				|  |  | +            try {
 | 
	
		
			
				|  |  | +                HttpEntity entity = response.getEntity();
 | 
	
		
			
				|  |  | +                if (entity != null) {
 | 
	
		
			
				|  |  | +                    EntityUtils.consume(entity);
 | 
	
		
			
				|  |  | +                }
 | 
	
		
			
				|  |  | +            } finally {
 | 
	
		
			
				|  |  | +                response.close();
 | 
	
		
			
				|  |  | +            }
 | 
	
		
			
				|  |  | +        } catch (ClientProtocolException e) {
 | 
	
		
			
				|  |  | +            e.printStackTrace();
 | 
	
		
			
				|  |  | +        } catch (UnsupportedEncodingException e1) {
 | 
	
		
			
				|  |  | +            e1.printStackTrace();
 | 
	
		
			
				|  |  | +        } catch (IOException e) {
 | 
	
		
			
				|  |  | +            e.printStackTrace();
 | 
	
		
			
				|  |  | +        }
 | 
	
		
			
				|  |  | +    }
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +    public String post(String urlString, JSONObject params) {
 | 
	
		
			
				|  |  | +        StringBuilder result = new StringBuilder();
 | 
	
		
			
				|  |  | +        // 创建默认的httpClient实例.    
 | 
	
		
			
				|  |  | +        CloseableHttpClient httpclient = this.getHttpClient();
 | 
	
		
			
				|  |  | +        if ("".equals(urlString) || urlString == null) {
 | 
	
		
			
				|  |  | +            return null;
 | 
	
		
			
				|  |  | +        }
 | 
	
		
			
				|  |  | +        // 创建httppost    
 | 
	
		
			
				|  |  | +        HttpPost httppost = new HttpPost(urlString);
 | 
	
		
			
				|  |  | +        // 创建参数队列   
 | 
	
		
			
				|  |  | +        httppost.addHeader("Content-type", "application/json; charset=utf-8");
 | 
	
		
			
				|  |  | +        httppost.setHeader("Accept", "application/json");
 | 
	
		
			
				|  |  | +        httppost.setEntity(new StringEntity(params.toJSONString(), Charset.forName("UTF-8")));
 | 
	
		
			
				|  |  | +        try {
 | 
	
		
			
				|  |  | +            LOGG.info("executing request " + httppost.getURI());
 | 
	
		
			
				|  |  | +            CloseableHttpResponse response = httpclient.execute(httppost);
 | 
	
		
			
				|  |  | +            HttpEntity entity = null;
 | 
	
		
			
				|  |  | +            try {
 | 
	
		
			
				|  |  | +                int httpCode = response.getStatusLine().getStatusCode();
 | 
	
		
			
				|  |  | +                if (httpCode == HttpURLConnection.HTTP_OK && response != null) {
 | 
	
		
			
				|  |  | +                    entity = response.getEntity();
 | 
	
		
			
				|  |  | +                    //读取服务器返回的json数据(接受json服务器数据)
 | 
	
		
			
				|  |  | +                    InputStream inputStream = entity.getContent();
 | 
	
		
			
				|  |  | +                    InputStreamReader inputStreamReader = new InputStreamReader(inputStream);
 | 
	
		
			
				|  |  | +                    // 读字符串用的。
 | 
	
		
			
				|  |  | +                    BufferedReader reader = new BufferedReader(inputStreamReader);
 | 
	
		
			
				|  |  | +                    String str;
 | 
	
		
			
				|  |  | +                    while ((str = reader.readLine()) != null) {
 | 
	
		
			
				|  |  | +                        result.append(str);
 | 
	
		
			
				|  |  | +                    }
 | 
	
		
			
				|  |  | +                    reader.close();
 | 
	
		
			
				|  |  | +                    return result.toString();
 | 
	
		
			
				|  |  | +                }
 | 
	
		
			
				|  |  | +            } finally {
 | 
	
		
			
				|  |  | +                EntityUtils.consume(entity);
 | 
	
		
			
				|  |  | +                response.close();
 | 
	
		
			
				|  |  | +            }
 | 
	
		
			
				|  |  | +        } catch (ClientProtocolException e) {
 | 
	
		
			
				|  |  | +            LOGG.error(e.getMessage(), e);
 | 
	
		
			
				|  |  | +        } catch (UnsupportedEncodingException e) {
 | 
	
		
			
				|  |  | +            LOGG.error(e.getMessage(), e);
 | 
	
		
			
				|  |  | +        } catch (IOException e) {
 | 
	
		
			
				|  |  | +            LOGG.error(e.getMessage(), e);
 | 
	
		
			
				|  |  | +        }
 | 
	
		
			
				|  |  | +        return null;
 | 
	
		
			
				|  |  | +    }
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +    /**
 | 
	
		
			
				|  |  | +     * 发送 get请求
 | 
	
		
			
				|  |  | +     */
 | 
	
		
			
				|  |  | +    public String get(String urlStr) {
 | 
	
		
			
				|  |  | +        StringBuilder result = new StringBuilder();
 | 
	
		
			
				|  |  | +        CloseableHttpClient httpclient = this.getHttpClient();
 | 
	
		
			
				|  |  | +        try {
 | 
	
		
			
				|  |  | +            // 创建httpget.
 | 
	
		
			
				|  |  | +            HttpGet httpget = new HttpGet(urlStr);
 | 
	
		
			
				|  |  | +            LOGG.info("executing request " + httpget.getURI());
 | 
	
		
			
				|  |  | +            // 执行get请求.
 | 
	
		
			
				|  |  | +            CloseableHttpResponse response = httpclient.execute(httpget);
 | 
	
		
			
				|  |  | +            HttpEntity entity = null;
 | 
	
		
			
				|  |  | +            try {
 | 
	
		
			
				|  |  | +                int httpCode = response.getStatusLine().getStatusCode();
 | 
	
		
			
				|  |  | +                if (httpCode == HttpURLConnection.HTTP_OK && response != null) {
 | 
	
		
			
				|  |  | +                    entity = response.getEntity();
 | 
	
		
			
				|  |  | +                    //读取服务器返回的json数据(接受json服务器数据)
 | 
	
		
			
				|  |  | +                    InputStream inputStream = entity.getContent();
 | 
	
		
			
				|  |  | +                    InputStreamReader inputStreamReader = new InputStreamReader(inputStream);
 | 
	
		
			
				|  |  | +                    // 读字符串用的。
 | 
	
		
			
				|  |  | +                    BufferedReader reader = new BufferedReader(inputStreamReader);
 | 
	
		
			
				|  |  | +                    String str;
 | 
	
		
			
				|  |  | +                    while ((str = reader.readLine()) != null) {
 | 
	
		
			
				|  |  | +                        result.append(str);
 | 
	
		
			
				|  |  | +                    }
 | 
	
		
			
				|  |  | +                    reader.close();
 | 
	
		
			
				|  |  | +                    return result.toString();
 | 
	
		
			
				|  |  | +                }
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +            } finally {
 | 
	
		
			
				|  |  | +                EntityUtils.consume(entity);
 | 
	
		
			
				|  |  | +                response.close();
 | 
	
		
			
				|  |  | +            }
 | 
	
		
			
				|  |  | +        } catch (ClientProtocolException e) {
 | 
	
		
			
				|  |  | +            e.printStackTrace();
 | 
	
		
			
				|  |  | +        } catch (ParseException e) {
 | 
	
		
			
				|  |  | +            e.printStackTrace();
 | 
	
		
			
				|  |  | +        } catch (IOException e) {
 | 
	
		
			
				|  |  | +            e.printStackTrace();
 | 
	
		
			
				|  |  | +        }
 | 
	
		
			
				|  |  | +        return null;
 | 
	
		
			
				|  |  | +    }
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +    /**
 | 
	
		
			
				|  |  | +     * 发送 get请求 有header
 | 
	
		
			
				|  |  | +     */
 | 
	
		
			
				|  |  | +    public String get(String urlStr,List<Map<String,String>> headerParams) {
 | 
	
		
			
				|  |  | +        StringBuilder result = new StringBuilder();
 | 
	
		
			
				|  |  | +        CloseableHttpClient httpclient = this.getHttpClient();
 | 
	
		
			
				|  |  | +        try {
 | 
	
		
			
				|  |  | +            // 创建httpget.
 | 
	
		
			
				|  |  | +            HttpGet httpget = new HttpGet(urlStr);
 | 
	
		
			
				|  |  | +            LOGG.info("executing request " + httpget.getURI());
 | 
	
		
			
				|  |  | +            if (headerParams != null && headerParams.size() > 0){
 | 
	
		
			
				|  |  | +                for (Map<String,String> map:headerParams){
 | 
	
		
			
				|  |  | +                    httpget.setHeader(map.get("name"),map.get("value"));
 | 
	
		
			
				|  |  | +                }
 | 
	
		
			
				|  |  | +            }
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +            // 执行get请求.
 | 
	
		
			
				|  |  | +            CloseableHttpResponse response = httpclient.execute(httpget);
 | 
	
		
			
				|  |  | +            HttpEntity entity = null;
 | 
	
		
			
				|  |  | +            try {
 | 
	
		
			
				|  |  | +                int httpCode = response.getStatusLine().getStatusCode();
 | 
	
		
			
				|  |  | +                log.info(String.valueOf(httpCode));
 | 
	
		
			
				|  |  | +                if (httpCode == HttpURLConnection.HTTP_OK && response != null) {
 | 
	
		
			
				|  |  | +                    entity = response.getEntity();
 | 
	
		
			
				|  |  | +                    //读取服务器返回的json数据(接受json服务器数据)
 | 
	
		
			
				|  |  | +                    InputStream inputStream = entity.getContent();
 | 
	
		
			
				|  |  | +                    InputStreamReader inputStreamReader = new InputStreamReader(inputStream);
 | 
	
		
			
				|  |  | +                    // 读字符串用的。
 | 
	
		
			
				|  |  | +                    BufferedReader reader = new BufferedReader(inputStreamReader);
 | 
	
		
			
				|  |  | +                    String str;
 | 
	
		
			
				|  |  | +                    while ((str = reader.readLine()) != null) {
 | 
	
		
			
				|  |  | +                        result.append(str);
 | 
	
		
			
				|  |  | +                    }
 | 
	
		
			
				|  |  | +                    reader.close();
 | 
	
		
			
				|  |  | +                    return result.toString();
 | 
	
		
			
				|  |  | +                }
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +            } finally {
 | 
	
		
			
				|  |  | +                EntityUtils.consume(entity);
 | 
	
		
			
				|  |  | +                response.close();
 | 
	
		
			
				|  |  | +            }
 | 
	
		
			
				|  |  | +        } catch (ClientProtocolException e) {
 | 
	
		
			
				|  |  | +            e.printStackTrace();
 | 
	
		
			
				|  |  | +        } catch (ParseException e) {
 | 
	
		
			
				|  |  | +            e.printStackTrace();
 | 
	
		
			
				|  |  | +        } catch (IOException e) {
 | 
	
		
			
				|  |  | +            e.printStackTrace();
 | 
	
		
			
				|  |  | +        }
 | 
	
		
			
				|  |  | +        return null;
 | 
	
		
			
				|  |  | +    }
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +    public String getUrlToImg(String urlStr,String fileName,String filePath) {
 | 
	
		
			
				|  |  | +        StringBuilder result = new StringBuilder();
 | 
	
		
			
				|  |  | +        CloseableHttpClient httpclient = this.getHttpClient();
 | 
	
		
			
				|  |  | +        try {
 | 
	
		
			
				|  |  | +            // 创建httpget.
 | 
	
		
			
				|  |  | +            HttpGet httpget = new HttpGet(urlStr);
 | 
	
		
			
				|  |  | +            LOGG.info("executing request " + httpget.getURI());
 | 
	
		
			
				|  |  | +            // 执行get请求.
 | 
	
		
			
				|  |  | +            CloseableHttpResponse response = httpclient.execute(httpget);
 | 
	
		
			
				|  |  | +            HttpEntity entity = null;
 | 
	
		
			
				|  |  | +            FileOutputStream fos = null;
 | 
	
		
			
				|  |  | +            try {
 | 
	
		
			
				|  |  | +                File file = new File(fileName);
 | 
	
		
			
				|  |  | +                int httpCode = response.getStatusLine().getStatusCode();
 | 
	
		
			
				|  |  | +                if (httpCode == HttpURLConnection.HTTP_OK && response != null) {
 | 
	
		
			
				|  |  | +                    entity = response.getEntity();
 | 
	
		
			
				|  |  | +                    //读取服务器返回的json数据(接受json服务器数据)
 | 
	
		
			
				|  |  | +                    InputStream inputStream = entity.getContent();
 | 
	
		
			
				|  |  | +                    Header header = entity.getContentType();
 | 
	
		
			
				|  |  | +                    log.info("==============>{}",header.getName());
 | 
	
		
			
				|  |  | +                    log.info("==============>{}",header.getValue());
 | 
	
		
			
				|  |  | +                    log.info("==============>{}",header.getElements());
 | 
	
		
			
				|  |  | +                    String type = header.getValue();
 | 
	
		
			
				|  |  | +                    String imageType="jpeg";
 | 
	
		
			
				|  |  | +                    if(type.indexOf("/")!=-1){
 | 
	
		
			
				|  |  | +                        int flagIndex = type.indexOf("/");
 | 
	
		
			
				|  |  | +                        imageType = type.substring(flagIndex + 1);
 | 
	
		
			
				|  |  | +                        if (!imageType.equals("gif")) {
 | 
	
		
			
				|  |  | +                            imageType = "jpeg";
 | 
	
		
			
				|  |  | +                        }
 | 
	
		
			
				|  |  | +                    }
 | 
	
		
			
				|  |  | +                    byte[] imageData = this.readInputStream(inputStream);
 | 
	
		
			
				|  |  | +                    log.info("filePath=====================>{}",filePath);
 | 
	
		
			
				|  |  | +                    File saveDir = new File(filePath);
 | 
	
		
			
				|  |  | +                    if (!saveDir.exists()) {
 | 
	
		
			
				|  |  | +                        saveDir.mkdir();
 | 
	
		
			
				|  |  | +                    }
 | 
	
		
			
				|  |  | +                    File saveFile = new File(saveDir +File.separator+fileName + "." + imageType);
 | 
	
		
			
				|  |  | +                    fos = new FileOutputStream(saveFile);
 | 
	
		
			
				|  |  | +                    fos.write(imageData);
 | 
	
		
			
				|  |  | +                    if (fos != null) {
 | 
	
		
			
				|  |  | +                        fos.flush();
 | 
	
		
			
				|  |  | +                        fos.close();
 | 
	
		
			
				|  |  | +                    }
 | 
	
		
			
				|  |  | +                    if (inputStream != null) {
 | 
	
		
			
				|  |  | +                        inputStream.close();
 | 
	
		
			
				|  |  | +                    }
 | 
	
		
			
				|  |  | +                    log.info("info:{}{}.{}download success",filePath,fileName,imageType);
 | 
	
		
			
				|  |  | +                    return imageType;
 | 
	
		
			
				|  |  | +                }
 | 
	
		
			
				|  |  | +            } finally {
 | 
	
		
			
				|  |  | +                EntityUtils.consume(entity);
 | 
	
		
			
				|  |  | +                response.close();
 | 
	
		
			
				|  |  | +                fos.close();
 | 
	
		
			
				|  |  | +            }
 | 
	
		
			
				|  |  | +        } catch (Exception e) {
 | 
	
		
			
				|  |  | +            e.printStackTrace();
 | 
	
		
			
				|  |  | +        }
 | 
	
		
			
				|  |  | +        return null;
 | 
	
		
			
				|  |  | +    }
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +    private byte[] readInputStream(InputStream inputStream) throws IOException {
 | 
	
		
			
				|  |  | +        byte[] buffer = new byte[1024];
 | 
	
		
			
				|  |  | +        int len = 0;
 | 
	
		
			
				|  |  | +        ByteArrayOutputStream bos = new ByteArrayOutputStream();
 | 
	
		
			
				|  |  | +        while ((len = inputStream.read(buffer)) != -1) {
 | 
	
		
			
				|  |  | +            bos.write(buffer, 0, len);
 | 
	
		
			
				|  |  | +        }
 | 
	
		
			
				|  |  | +        bos.close();
 | 
	
		
			
				|  |  | +        return bos.toByteArray();
 | 
	
		
			
				|  |  | +    }
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +}
 | 
	
		
			
				|  |  | +	
 | 
	
		
			
				|  |  | +	
 | 
	
		
			
				|  |  | +
 |