HttpsURLConnection
来源:互联网 发布:javascript 最新版本 编辑:程序博客网 时间:2024/04/30 11:45
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
- HttpsURLConnection
- HttpsURLConnection
- Java https请求 HttpsURLConnection
- HttpsURLConnection 与 HttpURLConnection
- HttpsURLConnection 与 HttpURLConnection
- HttpURLConnection 与 HttpsURLConnection
- Android中的HttpsURLConnection连接
- HttpsURLConnection 与 HttpURLConnection
- Android HttpsUrlConnection eofexception 错误
- Java https请求 HttpsURLConnection
- HttpsUrlConnection https双向验证
- JAVA HTTPS请求 HttpsURLConnection
- https 的 HttpsURLConnection
- Https-HttpsURLConnection 配置
- Android HttpsUrlConnection HTTPS
- HttpsURLConnection上传文件流
- HttpsURLConnection添加SSL认证
- HttpsURLConnection的使用
- HttpClient4.X Invalid use of SingleClientConnManager: connection still allocated
- retrofit 如何查看日志
- java 小结 (四)
- Invalid use of BasicClientConnManager: connection still allocated.
- Dubbo学习(六):多注册中心
- HttpsURLConnection
- java 实现两个日期之间所有日期的遍历
- java小程序之猜生日
- 关于Java Swing怎么实时刷新JTextArea,以显示刚才加append的内容(线程角度)
- Selenium 心跳时间
- CTreeCtrl 控件使用总结
- Java学习总结---反射
- exe4j打包jar文件成exe .
- leetcode 207. Course Schedule