赞
踩
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();
}
}
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。