Michael

写写代码,说说人生

您好,我是Michael,欢迎来到我的个人家园。
代码搬运工,目前就职于XX证券,努力修行中。


H5 / Java / Objc / Swift / Vue / RN

Android中HTTP网络请求相关问题

Google表示,为保证用户数据和设备的安全,针对下一代 Android 系统(Android P) 的应用程序,将要求默认使用加密连接,这意味着 Android P 将禁止 App 使用所有未加密的连接,因此运行 Android P 系统的安卓设备无论是接收或者发送流量,未来都不能明码传输,需要使用下一代(Transport Layer Security)传输层安全协议,而 Android Nougat 和 Oreo 则不受影响。

因此在Android 中 使用HttpUrlConnection进行http请求会出现以下异常:

W/System.err: java.io.IOException: Cleartext HTTP traffic to **** not permitted

使用OKHttp请求则出现:

java.net.UnknownServiceException: CLEARTEXT communication ** not permitted by network security policy

在Android P系统的设备上,如果应用使用的是非加密的明文流量的http网络请求,则会导致该应用无法进行网络请求,https则不会受影响,同样地,如果应用嵌套了webview,webview也只能使用https请求。

有人认为 Android P 上所有的 App 都需要使用 TLS 加密会降低上网体验,事实上这是一种误解,至于 App 对于少数旧服务器的连接如果非要使用明码传输,开发者需要更改 App 的网络安全配置以允许此类连接。

有以下三种解决方案

  • APP改用https请求

  • argetSdkVersion 降到27以下

  • 在 res 下新增一个 xml 目录,然后创建一个名为:network_security_config.xml 文件(名字自定) ,内容如下,大概意思就是允许开启http请求。

    <?xml version="1.0" encoding="utf-8"?>
    <network-security-config>
        <base-config cleartextTrafficPermitted="true" />
    </network-security-config>
    

    然后在APP的AndroidManifest.xml文件下的application标签增加以下属性。

    <application
    ...
     android:networkSecurityConfig="@xml/network_security_config"
    ...
    />
    

相关参考链接

网络其他方案描述

从Android 9开始,需要根据网络全进行相关配置(According to Network security configuration )。 Starting with Android 9.0 (API level 28), cleartext support is disabled by default.

  • 方案1
    • Create file res/xml/network_security_config.xml
      <?xml version="1.0" encoding="utf-8"?>
          <network-security-config>
      		<domain-config cleartextTrafficPermitted="true">
          		<domain includeSubdomains="true">Your URL(ex: 127.0.0.1)</domain>
          	</domain-config>
          </network-security-config>
      
    • 在AndroidManifest.xml增加一项配置
      <?xml version="1.0" encoding="utf-8"?>
      <manifest ...>
      	<uses-permission android:name="android.permission.INTERNET" />
      	<application
      		...
      		android:networkSecurityConfig="@xml/network_security_config"
      		...>
      		...
      	</application>
      </manifest>
      
  • 方案2 在AndroidManifest.xml中设置android:usesCleartextTraffic="true"即可。
    <?xml version="1.0" encoding="utf-8"?>
    <manifest ...>
    	<uses-permission android:name="android.permission.INTERNET" />
    	<application
    		...
    		android:usesCleartextTraffic="true"
    		...>
    		...
    	</application>
    </manifest>
    

    但这样设置会有一个问题,具体见下面:

    Also as @david.s’ answer pointed out android:targetSandboxVersion can be a problem too. According to Manifest Docs: android:targetSandboxVersion The target sandbox for this app to use. The higher the sandbox version number, the higher the level of security. Its default value is 1; you can also set it to 2. Setting this attribute to 2 switches the app to a different SELinux sandbox. The following restrictions apply to a level 2 sandbox:

    • The default value of usesCleartextTraffic in the Network Security Config is false.
    • Uid sharing is not permitted.
  • 方案3 如果在<manifest>中有配置android:targetSandboxVersion,将其减少到1. 即在 AndroidManifest.xml 文件中:
    <?xml version="1.0" encoding="utf-8"?>
    <manifest android:targetSandboxVersion="1">
    	<uses-permission android:name="android.permission.INTERNET" />
    	...
    </manifest>
    
最近的文章

Mac中Tomcat之startup.sh拒绝访问的问题

今天在Mac上启动Tomcat,结果弹出:-bash: ./startup.sh: Permission denied 的提示。这是因为用户没有权限,而导致无法执行。用命令 chmod 修改一下 bin 目录下的.sh权限就可以了。chmod u+x *.sh命令解释: u: 这里指文件所有者 +x: 添加可执行权限 *.sh:表示所有的sh文件…

Tomcat继续阅读
更早的文章

Charles使用教程

简介Charles是常用的截取网络封包的工具(俗称抓包)。Charles 通过将自己设置成系统的网络访问代理服务器,使得所有的网络访问请求都通过它来完成,从而实现了网络封包的截取和分析。 Charles 是收费软件,可以免费试用30 天。试用期过后,未付费的用户仍然可以继续使用,但是每次使用时间不能超过30 分钟,并且启动时将会有10 秒钟的延时。安装首先从Charles官网下载安装包进行安装。使用介绍Charles 主要提供两种查看封包的视图,分别名为 “Structure” 和 “Se...…

数据抓包工具继续阅读