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();

}

}

Java调用 HTTPS 协议的过程中,需要使用 SSLContext 和 HttpsURLConnection 类。SSLContext 类用于创建 SSL 连接,HttpsURLConnection 用于发送 HTTPS 请求。 下面是一个简单的示例代码: ```java import javax.net.ssl.HttpsURLConnection; import javax.net.ssl.SSLContext; import javax.net.ssl.TrustManager; import javax.net.ssl.X509TrustManager; import java.io.BufferedReader; import java.io.InputStreamReader; import java.net.URL; import java.security.cert.CertificateException; import java.security.cert.X509Certificate; public class HttpsClient { public static void main(String[] args) throws Exception { // 创建 SSL 连接 SSLContext sslContext = SSLContext.getInstance("TLS"); sslContext.init(null, new TrustManager[] { new X509TrustManager() { public void checkClientTrusted(X509Certificate[] chain, String authType) throws CertificateException {} public void checkServerTrusted(X509Certificate[] chain, String authType) throws CertificateException {} public X509Certificate[] getAcceptedIssuers() { return new X509Certificate[0]; } } }, null); // 打开 HTTPS 连接 URL url = new URL("https://example.com/api"); HttpsURLConnection conn = (HttpsURLConnection) url.openConnection(); conn.setSSLSocketFactory(sslContext.getSocketFactory()); // 发送 HTTPS 请求 BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream())); String line; while ((line = reader.readLine()) != null) { System.out.println(line); } reader.close(); } } ``` 在上面的示例代码中,我们创建了一个 SSLContext 对象,使用 TrustManager 来实现信任所有证书。然后,我们打开 HTTPS 连接,并将 SSLContext 对象的 SocketFactory 设置为连接的 SSLSocketFactory。最后,我们发送 HTTPS 请求,并读取响应内容。 需要注意的是,为了避免信任所有证书带来的安全风险,实际使用中应该使用自己的证书或信任指定的证书
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值