HttpsURLConnection

    import java.io.FileInputStream;  
    import java.io.InputStreamReader;  
    import java.net.InetSocketAddress;  
    import java.net.Proxy;  
    import java.net.SocketAddress;  
    import java.net.URL;  
    import java.security.KeyStore;  
    import java.security.SecureRandom;  
    import javax.net.ssl.HttpsURLConnection;  
    import javax.net.ssl.KeyManager;  
    import javax.net.ssl.KeyManagerFactory;  
    import javax.net.ssl.SSLContext;  
    import javax.net.ssl.TrustManager;  
      
    import com.ebiz.framework.BaseConst;  
      
    public class HttpUtil {  
      
        public void send() throws Exception {  
      
            KeyStore ks = initKeyStore("你的证书密码", "你的证书地址");  
            KeyManagerFactory keyManagerFactory = initKeyManagerFactory(ks, "你的证书密码");    
            SSLContext ssf = initSSLContext(keyManagerFactory.getKeyManagers(), null, new SecureRandom());  
            URL url = new URL("你的https地址");  
            HttpsURLConnection httpsConn = (HttpsURLConnection) url.openConnection();  
            httpsConn.setSSLSocketFactory(ssf.getSocketFactory());  
            httpsConn.setRequestMethod("POST");   
            httpsConn.setDoOutput(true);   
            httpsConn.setDoInput(true);  
            InputStreamReader insr = new InputStreamReader(httpsConn.getInputStream());  
            StringBuffer result = new StringBuffer();  
            // 读取服务器的响应内容并显示  
            int respInt = insr.read();  
            while (respInt != -1) {  
                result.append((char) respInt);  
                respInt = insr.read();  
            }  
            System.out.println(result.toString());  
        }  
      
      
        /** 
         * 初始化SSLContext 
         *  
         * @param keyManager 
         *            密钥管理器 
         * @param trustManager 
         *            信任管理器,判断返回请求 
         * @param secureRandom 
         *            随机数 
         * @return 
         * @throws Exception 
         */  
        public static SSLContext initSSLContext(KeyManager[] keyManager, TrustManager[] trustManager, SecureRandom secureRandom) throws Exception {  
            SSLContext sslContext = SSLContext.getInstance("SSL");  
            sslContext.init(keyManager, trustManager, secureRandom);  
            return sslContext;  
        }  
      
        /** 
         * 初始化密钥管理器 
         *  
         * @param ks 
         * @param keyPwd 
         * @return 
         * @throws Exception 
         */  
        public static KeyManagerFactory initKeyManagerFactory(KeyStore ks, String keyPwd) throws Exception {  
            KeyManagerFactory keyManagerFactory = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());// Sunx509  
            keyManagerFactory.init(ks, keyPwd.toCharArray());  
            return keyManagerFactory;  
        }  
      
        /** 
         * 初始化KeyStore. 
         *  
         * @param keyStorePath 
         *            密钥库路径 
         * @param password 
         *            密码 
         * @return 密钥库 
         * @throws Exception 
         */  
        public static KeyStore initKeyStore(String password, String keyStorePath) throws Exception {  
            // 实例化密钥库 JKS  
            KeyStore ks = KeyStore.getInstance(KeyStore.getDefaultType());  
            FileInputStream is = new FileInputStream(keyStorePath);  
            ks.load(is, password.toCharArray());  
            is.close();  
            return ks;  
        }  
      
        public static void main(String[] args) {  
            HttpUtil t = new HttpUtil ();  
            try {  
                t.send();  
            } catch (Exception e) {  
                e.printStackTrace();  
            }  
        }  
      
    }  

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值