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

 

你可能感兴趣的:(urlconnection)