Java中请求HTTPS加密的源代码

简介:  HTTPS访问方法的代码,抄一个能用的DEMO吧,我测试过的,可以用、,希望对大家也有用   import java.io.*;import java.net.*;import java.security.*;import java.security.cert.*;import java.util.*;import javax.net.ssl.*;public class Http

 HTTPS访问方法的代码,抄一个能用的DEMO吧,我测试过的,可以用、,希望对大家也有用

 

import  java.io. * ;
import  java.net. * ;
import  java.security. * ;
import  java.security.cert. * ;
import  java.util. * ;
import  javax.net.ssl. * ;

public   class  HttpsTest  {
    
// We would never hardcode this literal in a real system,
    
// this is only for this article.
    private String url = "https://www.paypal.com/cn";

    
// Create an anonymous class to trust all certificates.
    
// This is bad style, you should create a separate class.
    private X509TrustManager xtm = new X509TrustManager() {
        
public void checkClientTrusted(X509Certificate[] chain, String authType) {}

            
public void checkServerTrusted(X509Certificate[] chain, String authType) {
                System.out.println(
"cert: " + chain[0].toString() + ", authType: " + authType);
            }


            
public X509Certificate[] getAcceptedIssuers() {
                
return null;
            }

    }
; 

    
// Create an class to trust all hosts
    private HostnameVerifier hnv = new HostnameVerifier() {
        
public boolean verify(String hostname, SSLSession session) {
            System.out.println(
"hostname: " + hostname);
            
return true;
        }

    }
; 

    
// In this function we configure our system with a less stringent
    
// hostname verifier and X509 trust manager.  This code is
    
// executed once, and calls the static methods of HttpsURLConnection
    public HttpsTest() {
        
// Initialize the TLS SSLContext with
        
// our TrustManager
        SSLContext sslContext = null;

        
try {
            sslContext 
= SSLContext.getInstance("TLS");
            X509TrustManager[] xtmArray 
= new X509TrustManager[] { xtm };
            sslContext.init(
null, xtmArray, new java.security.SecureRandom());
        }
 catch(GeneralSecurityException gse) {
            
// Print out some error message and deal with this exception
        }


        
// Set the default SocketFactory and HostnameVerifier
        
// for javax.net.ssl.HttpsURLConnection
        if(sslContext != null) {
            HttpsURLConnection.setDefaultSSLSocketFactory(sslContext.getSocketFactory());
        }


        HttpsURLConnection.setDefaultHostnameVerifier(hnv);
    }


    
// This function is called periodically, the important thing
    
// to note here is that there is no special code that needs to
    
// be added to deal with a "HTTPS" URL.  All of the trust
    
// management, verification, is handled by the HttpsURLConnection.
    public void run() {
        
try {
            URLConnection urlCon 
= (new URL(url)).openConnection();
            BufferedReader in 
= new BufferedReader(new InputStreamReader(urlCon.getInputStream()));
            String line;

            
while((line = in.readLine()) != null) {
                System.out.println(line);
            }


        
//  Whatever we want to do with these quotes
        }
 catch(MalformedURLException mue) {
            mue.printStackTrace();
        }
 catch(IOException ioe) {
            ioe.printStackTrace();
        }
 catch(Exception e) {
            e.printStackTrace();
        }

    }


    
public static void main(String[] args) {
        HttpsTest httpsTest 
= new HttpsTest();
        httpsTest.run();
    }

}
 
相关文章
|
安全 算法 网络协议
解析:HTTPS通过SSL/TLS证书加密的原理与逻辑
HTTPS通过SSL/TLS证书加密,结合对称与非对称加密及数字证书验证实现安全通信。首先,服务器发送含公钥的数字证书,客户端验证其合法性后生成随机数并用公钥加密发送给服务器,双方据此生成相同的对称密钥。后续通信使用对称加密确保高效性和安全性。同时,数字证书验证服务器身份,防止中间人攻击;哈希算法和数字签名确保数据完整性,防止篡改。整个流程保障了身份认证、数据加密和完整性保护。
|
算法 安全 网络安全
https 的加密过程
HTTPS通过SSL/TLS协议实现安全通信,结合非对称加密与对称加密技术。客户端与服务器协商加密套件,验证证书后生成主密钥用于后续数据加密传输,确保身份真实、数据保密与完整。
2861 0
|
存储 Java 数据安全/隐私保护
Java技术栈揭秘:Base64加密和解密文件的实战案例
以上就是我们今天关于Java实现Base64编码和解码的实战案例介绍。希望能对你有所帮助。还有更多知识等待你去探索和学习,让我们一同努力,继续前行!
822 5
|
存储 缓存 Java
java语言后台管理ruoyi后台管理框架-登录提示“无效的会话,或者会话已过期,请重新登录。”-扩展知识数据库中密码加密的方法-问题如何解决-以及如何重置若依后台管理框架admin密码-优雅草卓伊凡
java语言后台管理ruoyi后台管理框架-登录提示“无效的会话,或者会话已过期,请重新登录。”-扩展知识数据库中密码加密的方法-问题如何解决-以及如何重置若依后台管理框架admin密码-优雅草卓伊凡
2827 3
java语言后台管理ruoyi后台管理框架-登录提示“无效的会话,或者会话已过期,请重新登录。”-扩展知识数据库中密码加密的方法-问题如何解决-以及如何重置若依后台管理框架admin密码-优雅草卓伊凡
|
安全 Linux 数据安全/隐私保护
python知识点100篇系列(15)-加密python源代码为pyd文件
【10月更文挑战第5天】为了保护Python源码不被查看,可将其编译成二进制文件(Windows下为.pyd,Linux下为.so)。以Python3.8为例,通过Cython工具,先写好Python代码并加入`# cython: language_level=3`指令,安装easycython库后,使用`easycython *.py`命令编译源文件,最终生成.pyd文件供直接导入使用。
1008 3
python知识点100篇系列(15)-加密python源代码为pyd文件
|
安全 算法 网络协议
【网络原理】——图解HTTPS如何加密(通俗简单易懂)
HTTPS加密过程,明文,密文,密钥,对称加密,非对称加密,公钥和私钥,证书加密
|
域名解析 算法 安全
免费申请https加密全攻略
访问JoySSL官网注册账号,申请免费SSL证书。选择证书类型,填写域名信息,生成CSR文件,验证域名所有权。下载并部署证书至服务器,测试HTTPS连接。注意定期续期,确保兼容性和安全性。如有问题,可联系JoySSL客服。
|
安全 网络安全 数据安全/隐私保护
内网IP地址实现HTTPS加密访问教程
在内网环境中,为确保数据传输的安全性,绑定SSL证书搭建HTTPS服务器至关重要。本文介绍了内网IP地址的前期准备、申请SSL证书的步骤以及客户端配置方法。具体包括选择合适的CA、注册账号、提交申请、下载证书,并在客户端导入根证书,确保通信数据的安全加密。推荐使用JoySSL提供的技术解决方案,确保内网设备通信安全。
内网IP地址实现HTTPS加密访问教程
|
安全 网络安全 数据安全/隐私保护
HTTPS中的加密算法
HTTPS中的加密算法
|
Java Maven 数据安全/隐私保护
如何实现Java打包程序的加密代码混淆,避免被反编译?
【10月更文挑战第15天】如何实现Java打包程序的加密代码混淆,避免被反编译?
4783 2