Android 9.0 HTTP请求报错
主要参考stackoverflow上hrishikesh-kadam的回答
1。在res
创建xml
文件夹创建network_security_config.xml
<?xml version="1.0" encoding="utf-8"?>
<network-security-config>
<domain-config cleartextTrafficPermitted="true">
<domain includeSubdomains="true">(你的http url如我写的)10.0.70.43</domain>
</domain-config>
<!--抓包需要的-->
<debug-overrides>
<trust-anchors>
<!-- Trust user added CAs while debuggable only -->
<certificates src="user" />
</trust-anchors>
</debug-overrides>
</network-security-config>
2。在AndroidManifest.xml
中添加下面代码
android:networkSecurityConfig="@xml/network_security_config"
3。我的https和http互换添加的一些
//下面代码看https://github.com/square/okhttp/wiki/HTTPS
//https用ConnectionSpec.MODERN_TLS,不是的就用ConnectionSpec.CLEARTEXT
ConnectionSpec spec = null;
if (BASEURL.startsWith("https")) {
spec = new ConnectionSpec.Builder(ConnectionSpec.MODERN_TLS)
.tlsVersions(TlsVersion.TLS_1_3,
TlsVersion.TLS_1_2)
.cipherSuites(
CipherSuite.TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,
CipherSuite.TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256,
CipherSuite.TLS_DHE_RSA_WITH_AES_128_GCM_SHA256)
.build();
} else {
spec = new ConnectionSpec.Builder(ConnectionSpec.CLEARTEXT)
.build();
}
OkHttpClient okHttpClient = new OkHttpClient.Builder()
.sslSocketFactory(sslParams.sSLSocketFactory, sslParams.trustManager)
.connectionSpecs(Collections.singletonList(spec))
.hostnameVerifier(new HttpsUtils.UnSafeHostnameVerifier())
还没有评论,来说两句吧...