Android 9.0强制使用https,会阻塞http请求,如果app使用的第三方sdk有http,将全部被阻塞

Android 9.0强制使用https,会阻塞http请求,如果app使用的第三方sdk有http,将全部被阻塞,第1张

概述原文链接:https://www.jianshu.com/p/5f65656d0a06Android9.0强制使用https,会阻塞http请求,如果app使用的第三方sdk有http,将全部被阻塞。出现UnknownServiceException:CLEARTEXTcommunicationtolocalhostnotpermittedbynetworksecuritypoli 原文链接:https://www.jianshu.com/p/5f65656d0a06

AndroID 9.0强制使用https,会阻塞http请求,如果app使用的第三方sdk有http,将全部被阻塞。@H_502_1@出现

UnkNownServiceException: CLEARTEXT communication to localhost not permitted by network security policy

或者

IOException java.io.IOException: Cleartext http traffic to * not permitted

就说明,你需要去兼容了。最简单的兼容方式是在AndroIDManifest文件的application设置

androID:usesCleartextTraffic="true"

这是第一种方式。@H_502_1@可以参考stackoverflow的回答

AndroID P - CLEARTEXT communication not permitted by network security policy

不允许明文通信,

第二种方式:网络安全性配置

在AndroIDManifest文件的application节点配置

androID:networkSecurityConfig="@xml/network_security_config"

xml中的具体配置。@H_502_1@可以参考我的配置

<?xml version="1.0" enCoding="utf-8"?><network-security-config>    <!--默认配置,明文通信,使用系统证书-->    <base-config cleartextTrafficPermitted="true">        <trust-anchors>            <!--trust system while release only-->            <certificates src="system" />        </trust-anchors>    </base-config>       <!--自己服务器,使用https,将所有域名添加到此-->    <domain-config cleartextTrafficPermitted="false">        <domain includeSubdomains="true">xxx.xxx.com</domain>        <!--<trust-anchors>-->        <!--<certificates src="@raw/my_proxy_ssl_proxying_certificate" />-->        <!--</trust-anchors>-->    </domain-config>    <!--deBUG模式,可以使用用户自己安装的正式,比如charles抓包安装的证书,这个配置只在 androID:deBUGgable 为 "true" 时将应用的重写,IDE 和构建工具生成的非发布版本通常属于此情况-->    <deBUG-overrIDes>        <!-- Trust user added CAs while deBUGgable only -->        <trust-anchors>            <certificates src="system" />            <certificates src="user" />        </trust-anchors>    </deBUG-overrIDes></network-security-config>

第二种方式才是最好的方式,具体参考官方文档security-config

还有就是如果在请求https的时候抛出SSLHandshakeException

javax.net.ssl.SSLHandshakeException:java.security.cert.CertPathValIDatorException: Trust anchor for certification path not found. 

请检查手机是不是设置了代理。Android7.0之后不特殊处理,是无法抓取https。@H_502_1@把androID官方文档放这里,可以自己查阅通过https和SSL确保安全(https://developer.android.com/training/articles/security-ssl?hl=zh-cn)

@H_502_1@@H_502_1@转载自链接:https://www.jianshu.com/p/5f65656d0a06

总结

以上是内存溢出为你收集整理的Android 9.0强制使用https,会阻塞http请求,如果app使用的第三方sdk有http,将全部被阻塞全部内容,希望文章能够帮你解决Android 9.0强制使用https,会阻塞http请求,如果app使用的第三方sdk有http,将全部被阻塞所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://www.54852.com/web/1111735.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-29
下一篇2022-05-29

发表评论

登录后才能评论

评论列表(0条)

    保存