Android 9.0 HTTP请求报错

Bertha 。 2022-02-21 08:57 319阅读 0赞

主要参考stackoverflow上hrishikesh-kadam的回答

1。在res创建xml文件夹创建network_security_config.xml

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <network-security-config>
  3. <domain-config cleartextTrafficPermitted="true">
  4. <domain includeSubdomains="true">(你的http url如我写的)10.0.70.43</domain>
  5. </domain-config>
  6. <!--抓包需要的-->
  7. <debug-overrides>
  8. <trust-anchors>
  9. <!-- Trust user added CAs while debuggable only -->
  10. <certificates src="user" />
  11. </trust-anchors>
  12. </debug-overrides>
  13. </network-security-config>

2。在AndroidManifest.xml中添加下面代码

  1. android:networkSecurityConfig="@xml/network_security_config"

3。我的https和http互换添加的一些

  1. //下面代码看https://github.com/square/okhttp/wiki/HTTPS
  2. //https用ConnectionSpec.MODERN_TLS,不是的就用ConnectionSpec.CLEARTEXT
  3. ConnectionSpec spec = null;
  4. if (BASEURL.startsWith("https")) {
  5. spec = new ConnectionSpec.Builder(ConnectionSpec.MODERN_TLS)
  6. .tlsVersions(TlsVersion.TLS_1_3,
  7. TlsVersion.TLS_1_2)
  8. .cipherSuites(
  9. CipherSuite.TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,
  10. CipherSuite.TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256,
  11. CipherSuite.TLS_DHE_RSA_WITH_AES_128_GCM_SHA256)
  12. .build();
  13. } else {
  14. spec = new ConnectionSpec.Builder(ConnectionSpec.CLEARTEXT)
  15. .build();
  16. }
  17. OkHttpClient okHttpClient = new OkHttpClient.Builder()
  18. .sslSocketFactory(sslParams.sSLSocketFactory, sslParams.trustManager)
  19. .connectionSpecs(Collections.singletonList(spec))
  20. .hostnameVerifier(new HttpsUtils.UnSafeHostnameVerifier())

发表评论

表情:
评论列表 (有 0 条评论,319人围观)

还没有评论,来说两句吧...

相关阅读