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(); } } }
相关推荐
本demo使用HttpsURLConnection方式的SSL双向认证,实现oauth2.0客户端请求方式,并且实现了普通post接口请求,及多图上传的post请求接口,做了网络请求的封装。
使用httpclient和httpsurlconnection两种方法访问https网址,同时分为验证证书和不验证证书两种方法
android studio实现HttpsURLConnection post请求的发送并接收
Android使用HttpClient和HttpsUrlConnection两种方式访问https网站
NULL 博文链接:https://forlan.iteye.com/blog/2404071
NULL 博文链接:https://410063005.iteye.com/blog/1751366
项目里包含一个访问tomcat 的https连接和访问普通url(www.google.com)的https连接 一定记得配置tomcat支持https 配置过程很容易 我的博客转载了方法 还有亲测jdk1.7 tomcat7.0可以
下面小编就为大家带来一篇HttpsURLConnection上传文件流(实例讲解)。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
HttpsUrlConnection android网络请求类 当然还有HttpUrlConnection对应http请求,下面以百度首页为例进行请求 写一个GET请求工具类 public class NetUtlis { public static String SendUrlRequest(String s){ ...
HttpsURLConnection-Post 使用HttpURLConnection的发布方法
主要介绍了Java下载https文件并上传到阿里云oss服务器,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
主要介绍了JAVA通过HttpURLConnection 上传和下载文件的方法,非常具有实用价值,需要的朋友可以参考下
用PD4ML把HTML转pdf,下载可以直接运行,包含了所需要的jar包,其中有我找了好久的页眉页脚,尤其是每页的页数显示,合成时插入图片等功能,需要的可以下载
导语:个人对网络连接接触的不多,在使用时自己发现一些问题,记录一下。 正文:我在使用HttpURLConnection.getResponseCode()的时候直接报错是IOException错误,responseCode = -1。一直想不明白,同一个程序我调用...
现在很多主流平台采用rest方式的OpenAPI,例如小程序、聚合接口、公司内部接口、对外接口、微信接口等,很多采用rest轻量级数据传输的方式。于是乎简单封装下rest请求api(其实就是两个简单Java类,呵呵),可以实现...
HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory()); // Create all-trusting host name verifier HostnameVerifier allHostsValid = new HostnameVerifier() { public boolean ...
封装httpurlconnection 自己动手丰衣足食
HttpsUrlConnection 支持tls1.2
本类可以用来访问https连接,带cookie的连接,重定向的连接,基于HttpsUrlConnection,不需要第三方库