Sample ssl certificate authentication code for calling https request in java
1. Obtain httpclient and ignore certificate authentication
public static CloseableHttpClient createSSLClientDefault() { try { SSLContext sslContext = new SSLContextBuilder().loadTrustMaterial(null, new TrustStrategy() { // Trust all certificates public boolean isTrusted(X509Certificate[] arg0, String arg1) throws CertificateException { return true; } }).build(); // Create a hostname validator to bypass hostname validation HostnameVerifier hostnameVerifier = ; // Create an SSL connection socket factory to apply a custom SSL context and hostname validator to HTTPS connections SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(sslContext, hostnameVerifier); // Create a custom CloseableHttpClient instance and apply the SSL connection socket factory to the HTTP client return ().setSSLSocketFactory(sslsf).build(); } catch (KeyManagementException e) { (); } catch (NoSuchAlgorithmException e) { (); } catch (KeyStoreException e) { (); } return (); }
2. Use put request to call
public static String put(String url, Map<String, String> header, String param) throws Exception { String result = ""; StringEntity entity = new StringEntity(param, "utf-8"); CloseableHttpClient httpClient = null; try { httpClient = createSSLClientDefault(); HttpPut httpPut = new HttpPut(url); if ((header)) { for (<String, String> entry : ()) { ((), ()); } } if (entity != null) { ("application/json; charset=utf-8"); (entity); } ("Start requesting https interface:" + url ); HttpResponse httpResponse = (httpPut); ("put request returns httpResponse result:" + httpResponse); HttpEntity resEntity = (); result = (resEntity, "UTF-8"); ("put request returns result:" + result); } catch (Exception e) { throw e; } finally { if (httpClient != null) { (); } } return result; }
Summarize
This is the article about calling https in Java to ignore ssl certificate authentication. For more related Java to call https request and ignore ssl certificate authentication, please search for my previous article or continue browsing the related articles below. I hope everyone will support me in the future!
Related Articles
Implementing pure Java version SDK components based on logback
This article mainly introduces the SDK component that implements a pure Java version based on logback. During the project development process, logback is usually used as a dependency tool for logging. The usage method is to introduce logback-related jar packages and then configure configuration files to implement them. Friends who need it can refer to it.2023-11-11Detailed explanation of the adapter mode of java design pattern
This article mainly introduces the detailed explanation of the adapter mode of Java design mode. Friends who need it can refer to it2017-07-07How to implement Java access to Https requests through certificates
This article mainly introduces how Java can access Https requests through certificates, which is of good reference value and hopes to be helpful to everyone. If there are any mistakes or no complete considerations, I hope you will be very grateful for your advice2022-01-01Java daily exercises, make a little progress every day (49)
Below, the editor will bring you a few basic Java exercises (share). The editor thinks it is quite good, so I will share it with you now and give you a reference. Let's take a look with me, I hope it can help you2021-08-08Sample code for Java implementing PDF export function
This article mainly introduces the relevant knowledge of Java implementing PDF export function. The sample code in the article is explained in detail, which has certain learning value. Interested friends can learn about it.2023-09-09MyBatis+MySQL method to return the inserted primary key ID
This article mainly introduces the method of MyBatis+MySQL to return the inserted primary key ID, which has certain reference value. Interested friends can refer to it.2017-04-04Detailed explanation of Java's understanding of polymorphism
Hello everyone, this article mainly talks about the detailed explanation of Java's understanding of polymorphisms. Interested students, come and take a look. If it is helpful to you, remember to bookmark it for the convenience of browsing next time.2021-12-12Detailed explanation of Java string splicing
This article mainly introduces the knowledge about splicing Java strings. Here we have compiled relevant information and simple sample code. Interested friends can refer to it.2016-08-08Detailed explanation of the Java SSM project deployment and online configuration method (Ali Cloud Server ECS + Cloud Database RDS MySQL) (Pagoda)
This article mainly introduces the graphic tutorial of Java SSM project deployment and launch (Ali Cloud Server ECS + Cloud Database RDS MySQL) (Pagoda). This article introduces you very detailedly through the form of pictures and texts. Interested friends can take a look.2024-01-01Detailed explanation of basic java methods
This article mainly introduces the detailed explanation of the basic methods of java. There are very detailed code examples in the article, which are very helpful to friends who are learning basic java. Friends who need it can refer to it.2021-04-04