当前位置:   article > 正文

java请求https 证书_Java调用https接口,避免证书的方式

java请求https 证书_Java调用https接口,避免证书的方式

package com.moy.whymoy.test;

import org.apache.http.HttpEntity;

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.NoopHostnameVerifier;

import org.apache.http.conn.ssl.SSLConnectionSocketFactory;

import org.apache.http.conn.ssl.TrustStrategy;

import org.apache.http.entity.mime.MultipartEntityBuilder;

import org.apache.http.impl.client.CloseableHttpClient;

import org.apache.http.impl.client.HttpClients;

import org.apache.http.ssl.SSLContexts;

import org.apache.http.util.EntityUtils;

import javax.net.ssl.*;

import java.io.*;

import java.net.URL;

import java.security.KeyManagementException;

import java.security.KeyStoreException;

import java.security.NoSuchAlgorithmException;

import java.security.cert.CertificateException;

import java.security.cert.X509Certificate;/**

* [Project]:whymoy

* [Email]:moy25@foxmail.com

* [Date]:2018/3/14

* [Description]:

*

* @author YeXiangYang*/

public classMain {public static voidmain(String[] args) throws IOException, NoSuchAlgorithmException, KeyStoreException, KeyManagementException {

String url= "https://kyfw.12306.cn/otn/";try (CloseableHttpClient httpClient =createHttpClient()) {

HttpGet httpGet= newHttpGet(url);try (CloseableHttpResponse httpResponse =httpClient.execute(httpGet)) {

HttpEntity entity=httpResponse.getEntity();

String result=EntityUtils.toString(entity);

EntityUtils.consume(entity);

System.out.printf(result);

}

}

}private staticCloseableHttpClient createHttpClient() throws KeyStoreException, NoSuchAlgorithmException, KeyManagementException {

SSLContext sslcontext=SSLContexts.custom()

.loadTrustMaterial(null, (chain, authType) -> true)

.build();

SSLConnectionSocketFactory sslSf= new SSLConnectionSocketFactory(sslcontext, null, null,newNoopHostnameVerifier());returnHttpClients.custom().setSSLSocketFactory(sslSf).build();

}

}

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/羊村懒王/article/detail/732663
推荐阅读
相关标签
  

闽ICP备14008679号