




下载本文档
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
【移动应用开发技术】Android如何使用OkHttp请求自签名的https网站
HTTPS的工作原理使用OKHTTP请求自签名的https服务器数据/upload/information/20200623/125/126365.png/upload/information/20200623/125/126366.pngpackage
com.alpha58.okhttp;
import
android.content.Context;
import
java.io.IOException;
import
java.io.InputStream;
import
java.security.GeneralSecurityException;
import
java.security.KeyStore;
import
java.security.cert.Certificate;
import
java.security.cert.CertificateFactory;
import
java.util.Arrays;
import
java.util.Collection;
import
.ssl.KeyManagerFactory;
import
.ssl.SSLContext;
import
.ssl.SSLSocketFactory;
import
.ssl.TrustManager;
import
.ssl.TrustManagerFactory;
import
.ssl.X509TrustManager;
import
okhttp3.OkHttpClient;
/**
*
Created
by
admin
on
2017/03/12.
*/
public
final
class
HTTPSUtils
{
private
OkHttpClient
client;
public
Context
mContext;
/**
*
获取OkHttpClient实例
*
@return
*/
public
OkHttpClient
getInstance()
{
return
client;
}
/**
*
初始化HTTPS,添加信任证书
*
@param
context
*/
public
HTTPSUtils(Context
context)
{
mContext
=
context;
X509TrustManager
trustManager;
SSLSocketFactory
sslSocketFactory;
final
InputStream
inputStream;
try
{
inputStream
=
mContext.getAssets().open("srca.cer");
//
得到证书的输入流
try
{
trustManager
=
trustManagerForCertificates(inputStream);//以流的方式读入证书
SSLContext
sslContext
=
SSLContext.getInstance("TLS");
sslContext.init(null,
new
TrustManager[]{trustManager},
null);
sslSocketFactory
=
sslContext.getSocketFactory();
}
catch
(GeneralSecurityException
e)
{
throw
new
RuntimeException(e);
}
client
=
new
OkHttpClient.Builder()
.sslSocketFactory(sslSocketFactory,
trustManager)
.build();
}
catch
(IOException
e)
{
e.printStackTrace();
}
}
/**
*
以流的方式添加信任证书
*/
/**
*
Returns
a
trust
manager
that
trusts
{@code
certificates}
and
none
other.
HTTPS
services
whose
*
certificates
have
not
been
signed
by
these
certificates
will
fail
with
a
{@code
*
SSLHandshakeException}.
*
<p>
*
<p>This
can
be
used
to
replace
the
host
platform's
built-in
trusted
certificates
with
a
custom
*
set.
This
is
useful
in
development
where
certificate
authority-trusted
certificates
aren't
*
available.
Or
in
production,
to
avoid
reliance
on
third-party
certificate
authorities.
*
<p>
*
<p>
*
<h4>Warning:
Customizing
Trusted
Certificates
is
Dangerous!</h4>
*
<p>
*
<p>Relying
on
your
own
trusted
certificates
limits
your
server
team's
ability
to
update
their
*
TLS
certificates.
By
installing
a
specific
set
of
trusted
certificates,
you
take
on
additional
*
operational
complexity
and
limit
your
ability
to
migrate
between
certificate
authorities.
Do
*
not
use
custom
trusted
certificates
in
production
without
the
blessing
of
your
server's
TLS
*
administrator.
*/
private
X509TrustManager
trustManagerForCertificates(InputStream
in)
throws
GeneralSecurityException
{
CertificateFactory
certificateFactory
=
CertificateFactory.getInstance("X.509");
Collection<?
extends
Certificate>
certificates
=
certificateFactory.generateCertificates(in);
if
(certificates.isEmpty())
{
throw
new
IllegalArgumentException("expected
non-empty
set
of
trusted
certificates");
}
//
Put
the
certificates
a
key
store.
char[]
password
=
"password".toCharArray();
//
Any
password
will
work.
KeyStore
keyStore
=
newEmptyKeyStore(password);
int
index
=
0;
for
(Certificate
certificate
:
certificates)
{
String
certificateAlias
=
Integer.toString(index++);
keyStore.setCertificateEntry(certificateAlias,
certificate);
}
//
Use
it
to
build
an
X509
trust
manager.
KeyManagerFactory
keyManagerFactory
=
KeyManagerFactory.getInstance(
KeyManagerFactory.getDefaultAlgorithm());
keyManagerFactory.init(keyStore,
password);
TrustManagerFactory
trustManagerFactory
=
TrustManagerFactory.getInstance(
TrustManagerFactory.getDefaultAlgorithm());
trustManagerFactory.init(keyStore);
TrustManager[]
trustManagers
=
trustManagerFactory.getTrustManagers();
if
(trustManagers.length
!=
1
||
!(trustManagers[0]
instanceof
X509TrustManager))
{
throw
new
IllegalStateException("Unexpected
default
trust
managers:"
+
Arrays.toString(trustManagers));
}
return
(X509TrustManager)
trustManagers[0];
}
/**
*
添加password
*
@param
password
*
@return
*
@throws
GeneralSecurityException
*/
private
KeyStore
newEmptyKeyStore(char[]
password)
throws
GeneralSecurityException
{
try
{
KeyStore
keyStore
=
KeyStore.getInstance(KeyStore.getDefaultType());
//
这里添加自定义的密码,默认
InputStream
in
=
null;
//
By
convention,
'null'
creates
an
empty
key
store.
keyStore.load(in,
password);
return
keyStore;
}
catch
(IOException
e)
{
throw
new
AssertionError(e);
}
}
}public
void
getHttpsHtml(View
view)
{
Request
request
=
new
Request.Builder()
.url("/otn/")
.build();
HTTPSUtils
httpsUtils
=
new
HTTPSUtils(this);
httpsUtils.getInstance().newCall(request).enqueue(new
Callback()
{
@Override
public
void
onFailure(Call
call,
IOException
e)
{
System.o
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 潜水青少年挑战赛行业跨境出海项目商业计划书
- 2025至2031年中国大楼共用式缓降管行业投资前景及策略咨询研究报告
- 2025至2031年中国半自动多枪滚筒式干喷砂机行业投资前景及策略咨询研究报告
- 2025至2031年中国力凯水务管理系统行业投资前景及策略咨询研究报告
- 未来科技营行业深度调研及发展项目商业计划书
- 演讲大赛辅导行业深度调研及发展项目商业计划书
- 农业主题公园与游乐场行业深度调研及发展项目商业计划书
- 电力施工现场风险评估及安全措施
- 2025年物流公司财务运营计划范文
- 2025~2025苏少版《美术》四年级上册校外活动教学计划
- 材料力学-山东科技大学中国大学mooc课后章节答案期末考试题库2023年
- 教育行业教师外派管理规定
- C919飞机首飞试飞机组培训-指示记录
- 展览馆室内布展施工方案
- 济南大学《工程伦理与项目管理》2021-2022学年第一学期期末试卷
- 气压传动课件 项目八任务二 钻床自动化流水线气动系统
- 正规个人租车合同模板
- 《地方导游基础知识》8.1 港澳台 地方导游基础知识-题库及答案
- 2022年版信息科技新课标《义务教育信息科技课程标准(2022年版)》解读课件
- 财务岗位招聘面试题及回答建议(某大型国企)2025年
- VDA6.3 2023 过程审核检查表-参考表单
评论
0/150
提交评论