json所在的位置 (E:\apache-tomcat-9.0.22\webapps\ROOT)
tomcat配置参考地址
tomcat ssl配置 参考文档地址
生成keystore (keytool -genkeypair -alias alipay -keyalg RSA -validity 3650 -keystore alipay.keystore)
配置service.xml信息
[图片上传失败...(image-4faa9f-1595388338261)]
导出cer(keytool -exportcert -alias alipay -file alipay.cer -keystore alipay.keystore)
测试代码结构
具体实体类
class MainActivity : AppCompatActivity() {
private val mButton : Button by lazy {
btn
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
mButton.onClick {
sendHttps()
}
}
var sendHttps ={
doAsync {
//查看谷歌网址:https://developer.android.google.cn/training/articles/security-ssl?hl=zh_cn
//创建ssl上下文对象设置信任管理器(相当于保安)
val sslContext = SSLContext.getInstance("TLS")
val tmf = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm())
//初始化工厂
val ks = KeyStore.getInstance(KeyStore.getDefaultType())
//清空默认证书,设置自己证书
ks.load(null)
val cf = CertificateFactory.getInstance("X.509")
val open = assets.open("alipay.cer")
val cer = cf.generateCertificate(open)
ks.setCertificateEntry("alipay",cer)
tmf.init(ks)
val tm = tmf.trustManagers
sslContext.init(null,tm,null)
var url = URL("https://127.0.0.1:8443/list/alipay.json")
var connection:HttpsURLConnection = url.openConnection() as HttpsURLConnection
//使用信任管理器
connection.sslSocketFactory = sslContext.socketFactory
//主机名校验
connection.hostnameVerifier = MyHostname()
var ins = connection.inputStream
var bos = ByteArrayOutputStream()
var buffer = ByteArray(1024)
var len = 0
len = ins.read(buffer)
while (len != -1){
bos.write(buffer,0,len)
len = ins.read(buffer)
}
val result = bos.toString()
println("服务器返回数据 : " + result)
}
}
private class MyHostname : HostnameVerifier{
//主机名校验
override fun verify(hostname: String?, session: SSLSession?): Boolean {
return true
}
}
}