• HttpClient4.3.6 实现https访问


    package httptest;
    
    import java.io.IOException;
    import java.nio.charset.Charset;
    import java.security.KeyManagementException;
    import java.security.KeyStoreException;
    import java.security.NoSuchAlgorithmException;
    import java.security.cert.X509Certificate;
    import java.util.ArrayList;
    import java.util.List;
    import java.util.Map;
    import java.util.logging.Level;
    import java.util.logging.Logger;
    import javax.net.ssl.SSLContext;
    import org.apache.http.HttpEntity;
    import org.apache.http.NameValuePair;
    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.conn.ssl.SSLConnectionSocketFactory;
    import org.apache.http.conn.ssl.SSLContextBuilder;
    import org.apache.http.conn.ssl.TrustStrategy;
    import org.apache.http.impl.client.CloseableHttpClient;
    import org.apache.http.impl.client.HttpClients;
    import org.apache.http.message.BasicNameValuePair;
    import org.apache.http.util.EntityUtils;
    
    /**
     * @author yan
     * @date 2016-6-9 11:03:04
     * @version V1.0
     * @desc
     */
    public class HttpsUtil {
    
        public static final String get(final String url, final Map<String, Object> params) {
            StringBuilder sb = new StringBuilder("");
    
            if (null != params && !params.isEmpty()) {
                int i = 0;
                for (String key : params.keySet()) {
                    if (i == 0) {
                        sb.append("?");
                    } else {
                        sb.append("&");
                    }
                    sb.append(key).append("=").append(params.get(key));
                    i++;
                }
            }
    
            CloseableHttpClient httpClient = createSSLClientDefault();
    
            CloseableHttpResponse response = null;
            HttpGet get = new HttpGet(url + sb.toString());
            String result = "";
    
            try {
                response = httpClient.execute(get);
    
                if (response.getStatusLine().getStatusCode() == 200) {
                    HttpEntity entity = response.getEntity();
                    if (null != entity) {
                        result = EntityUtils.toString(entity, "UTF-8");
                    }
                }
            } catch (IOException ex) {
                Logger.getLogger(HttpsUtil.class.getName()).log(Level.SEVERE, null, ex);
            } finally {
                if (null != response) {
                    try {
                        EntityUtils.consume(response.getEntity());
                    } catch (IOException ex) {
                        Logger.getLogger(HttpsUtil.class.getName()).log(Level.SEVERE, null, ex);
                    }
                }
            }
    
            return result;
        }
    
        public static final String post(final String url, final Map<String, Object> params) {
            CloseableHttpClient httpClient = createSSLClientDefault();
            HttpPost post = new HttpPost(url);
    
            CloseableHttpResponse response = null;
    
            if (null != params && !params.isEmpty()) {
                List<NameValuePair> nvpList = new ArrayList<NameValuePair>();
                for (Map.Entry<String, Object> entry : params.entrySet()) {
                    NameValuePair nvp = new BasicNameValuePair(entry.getKey(), entry.getValue().toString());
                    nvpList.add(nvp);
                }
                post.setEntity(new UrlEncodedFormEntity(nvpList, Charset.forName("UTF-8")));
            }
    
            String result = "";
    
            try {
                response = httpClient.execute(post);
    
                if (response.getStatusLine().getStatusCode() == 200) {
                    HttpEntity entity = response.getEntity();
                    if (null != entity) {
                        result = EntityUtils.toString(entity, "UTF-8");
                    }
                }
            } catch (IOException ex) {
                Logger.getLogger(HttpsUtil.class.getName()).log(Level.SEVERE, null, ex);
            } finally {
                if (null != response) {
                    try {
                        EntityUtils.consume(response.getEntity());
                    } catch (IOException ex) {
                        Logger.getLogger(HttpsUtil.class.getName()).log(Level.SEVERE, null, ex);
                    }
                }
            }
    
            return result;
        }
    
        private static CloseableHttpClient createSSLClientDefault() {
    
            SSLContext sslContext;
            try {
                sslContext = new SSLContextBuilder().loadTrustMaterial(null, new TrustStrategy() {
                    //信任所有
                    @Override
                    public boolean isTrusted(X509Certificate[] xcs, String string){
                        return true;
                    }
                }).build();
    
                SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(sslContext);
    
                return HttpClients.custom().setSSLSocketFactory(sslsf).build();
            } catch (KeyStoreException ex) {
                Logger.getLogger(HttpsUtil.class.getName()).log(Level.SEVERE, null, ex);
            } catch (NoSuchAlgorithmException ex) {
                Logger.getLogger(HttpsUtil.class.getName()).log(Level.SEVERE, null, ex);
            } catch (KeyManagementException ex) {
                Logger.getLogger(HttpsUtil.class.getName()).log(Level.SEVERE, null, ex);
            }
    
            return HttpClients.createDefault();
        }
    
        public static void main(String[] args) {
            System.out.println("Result:" + get("https://github.com/", null));
        }
    }
  • 相关阅读:
    分期付款购买固定资产账务处理
    会计要素计量
    接受现金捐赠分录
    分配股票股利的分录
    R语言代写对用电负荷时间序列数据进行K-medoids聚类建模和GAM回归
    R语言代写用随机森林和文本挖掘提高航空公司客户满意度
    R语言代写时间序列TAR阈值模型分析 2
    R语言代写时间序列TAR阈值模型分析
    R语言代写文本挖掘tf-idf,主题建模,情感分析,n-gram建模研究
    R语言代写文本挖掘NASA数据网络分析,tf-idf和主题建模
  • 原文地址:https://www.cnblogs.com/yshyee/p/5572142.html
Copyright © 2020-2023  润新知