android ssl http,Android SSL HTTP请求使用自签名证书和CA
可以完成的任务,也使用DefaultHttpClient,即使here is suggested到:
身高HttpURLConnection的新代码
讲究也进口或添加证书到您的应用程序,因为你可能有在证书过期时更新证书存在问题。
这里如何获得DefaultHttpClient信任自签名证书:
* This method returns the appropriate HttpClient.
* @param isTLS Whether Transport Layer Security is required.
* @param trustStoreInputStream The InputStream generated from the BKS keystore.
* @param trustStorePsw The password related to the keystore.
* @return The DefaultHttpClient object used to invoke execute(request) method.
private DefaultHttpClient getHttpClient(boolean isTLS, InputStream trustStoreInputStream, String trustStorePsw)
throws KeyStoreException, NoSuchAlgorithmException, CertificateException, IOException, KeyManagementException, UnrecoverableKeyException {
DefaultHttpClient client = null;
SchemeRegistry schemeRegistry = new SchemeRegistry();
Scheme http = new Scheme("http", PlainSocketFactory.getSocketFactory(), 8080);
schemeRegistry.register(http);
if(isTLS) {
KeyStore trustKeyStore = null;
char[] trustStorePswCharArray = null;
if(trustStorePsw!=null) {
trustStorePswCharArray = trustStorePsw.toCharArray();
}
trustKeyStore = KeyStore.getInstance("BKS");
trustKeyStore.load(trustStoreInputStream, trustStorePswCharArray);
SSLSocketFactory sslSocketFactory = null;
sslSocketFactory = new SSLSocketFactory(trustKeyStore);
Scheme https = new Scheme("https", sslSocketFactory, 8443);
schemeRegistry.register(https);
}
HttpParams httpParams = new BasicHttpParams();
HttpConnectionParams.setConnectionTimeout(httpParams, CONNECTION_TIMEOUT);
HttpConnectionParams.setSoTimeout(httpParams, SOCKET_TIMEOUT);
ClientConnectionManager clientConnectionManager = new ThreadSafeClientConnManager(httpParams, schemeRegistry);
client = new DefaultHttpClient(clientConnectionManager, httpParams);
return client;
}
这里如何获得HttpsURLConnection:
* This method set the certificate for the HttpsURLConnection
* @param url The url to contact.
* @param certificateInputStream The InputStream generated from the .crt certificate.
* @param certAlias The alias for the certificate.
* @return The returned HttpsURLConnection
private HttpsURLConnection getHttpsURLConnection(URL url, InputStream certificateInputStream, String certAlias)
throws CertificateException, IOException, KeyStoreException, NoSuchAlgorithmException, KeyManagementException {
HttpsURLConnection connection = null;
CertificateFactory certFactory = null;
Certificate cert = null;
KeyStore keyStore = null;
TrustManagerFactory tmFactory = null;
SSLContext sslContext = null;
// Load certificates from an InputStream
certFactory = CertificateFactory.getInstance("X.509");
cert = certFactory.generateCertificate(certificateInputStream);
certificateInputStream.close();
// Create a KeyStore containing the trusted certificates
keyStore = KeyStore.getInstance(KeyStore.getDefaultType());
keyStore.load(null, null);
keyStore.setCertificateEntry(certAlias, cert);
// Create a TrustManager that trusts the certificates in our KeyStore
tmFactory = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
tmFactory.init(keyStore);
// Create an SSLContext that uses our TrustManager
sslContext = SSLContext.getInstance("TLS");
sslContext.init(null, tmFactory.getTrustManagers(), null);
connection = (HttpsURLConnection)url.openConnection();
connection.setSSLSocketFactory(sslContext.getSocketFactory());
return connection;
}
android ssl http,Android SSL HTTP请求使用自签名证书和CA相关推荐
- Android签名证书的生成
本节只针对如何从零开始实现简单的打包签名 目录 概要 Keytool生成JKS签名证书 Android Studio生成JKS签名证书 AS版本信息 签名证书的生成 签名打包 概要 debug.key ...
- 沃通开年采购季,SSL证书、代码签名证书特惠低价,助力保障关键数据安全
近期<关键信息基础设施安全保护条例>.<网络安全审查办法>等一系列政策的实施,今年国家对网络数据安全将更为重视,如何构建网络安全体系.保障企业数据安全将会是今年众多企业的重点工 ...
- python 证书-Python 发送带自签名证书的 https 请求
在拥有 .pfx 文件和其密码(若有加密)的前提下进行 https 请求 (关于这些文件的说明,参考:https://blog.51cto.com/wushank/1915795): 所有方法均忽略了 ...
- Android 抓包相关 SSL相关
https无法明文抓包 Android P版本开始强制App使用Https协议. 可参阅: Android 9.0强制使用https,会阻塞http请求,如果app使用的第三方sdk有http,将全部 ...
- Android中使用Webview SSL 自签名CA证书安全校验方案
前言: 因为我们的项目是Android + H5,之前的做法是把H5所需要的资源和html下载到本地这样证书校验不会走系统浏览器层只需要项目中预埋根证书就可以了,但是如果用webview加载线上的域名 ...
- Android手机发生了SSL连接错误该怎么解决
Android手机在国内的市场占有率是非常高的,使用的人非常多,经常会有人反应当Android手机在浏览网页时会出现"您的连接不是私密的"."此站点证书不受信任" ...
- Android手机上的SSL连接错误怎么解决?
SSL连接错误已经逐渐成为Android设备上最常见的问题之一,通常我们会收到提醒"您的连接不是私密的"."此站点证书不受信任"等等.这些安全提示甚至会出现在最 ...
- Android之javax.net.ssl.SSLPeerUnverifiedException: Hostname ip not verified:解决办法
1.问题 用HttpURLConnection去请求的时候抛了下面的异常 HttpRequest$HttpRequestException: javax.net.ssl.SSLPeerUnverifi ...
- android客户端Netty部署SSL/TLS
应用场景: 服务端server为windows系统通过kJava SDK附带的keytool创建了一个新的Keystore,秘钥及签名证书生成 (1)生成Netty服务器公钥.私钥和证书仓库: key ...
最新文章
- 步步为营 .NET 代码重构学习笔记 十一
- MySQL 下载与配置教程(免安装版)
- project 模板_不会绘制横道图?18个施工进度计划横道图模板,可一键自动生成,方便快捷易操作,直观形象,相当好用...
- RestTemplate入门案例
- 客户说发货慢怎么回复_女生微信说身体不舒服怎么回复关心她?
- Dijkstra模板题——单源最短路径(洛谷 P3371)
- VB6.0视频教程78集,入门视频教程,基础够了(基础篇)
- Python十大装B语法
- 电子银行卡的优势分析
- 有一个字符串,如11.2美元34人民币;如何将数字与单位分开,放入数组中呢,数组比如 attr[0]=11.2 attr[1]=美元 ,依次类推
- 【小说】玻璃碎片-第二章
- 计量经济学及Stata应用 陈强 第十章工具变量法习题10.6
- 手把手教你用Arcgis绘制地图【热力图】
- 4.四大组件的工作过程
- opencv+dlib实现给蒙娜丽莎“配”眼镜
- CSS太阳地球月亮转圈圈loading
- 出现ClassNotFoundException问题
- phpexcel设置背景色和边框和背景色注意事项
- Python爬取百思不得姐的视频
- 【DockerCE】Docker-CE 23.0.4正式版发布
热门文章
- 无心剑英译朱自清《匆匆》
- 【BZOJ4542】大数, 莫队
- 2017.10.28 管道取珠 失败总结
- Intel超线程技术 Hyper-Threading Technology (3) - 处理器资源与超线程(复制的资源)
- python画椭圆形_手残党福音:用Python画出机器人Dev
- 在Unity内利用混融公式剔除背景颜色导出透明PNG以及半透明遮挡相关问题的研究
- Unreal Engine 4 的 光和影
- shell与export命令
- MAC-VBox-Centos6.6安装增强功能
- Mybatis学习笔记-CURD(基于配置文件的方式)