Android; OkHttpClient 不支持 httpS
Android; OkHttpClient not working with httpS
目前我的 httpclient
只用于 http requests
,但现在我也想使用 https
。
我将 http URL
更改为 https URL
,但我的应用程序无法连接到 url。
仅供参考:我可以用我的浏览器连接到 https url
并得到我的响应,只是不在我的应用程序本身
有什么想法可能是什么问题或我做错了什么吗?
OkHttpClient okHttpClient = new OkHttpClient.Builder()
.connectTimeout(4, TimeUnit.SECONDS)
.build();
retrofit = new Retrofit.Builder()
.baseUrl("https:xyz.com/api/")
.addConverterFactory(GsonConverterFactory.create())
.client(okHttpClient)
.build();
为了能够执行 HTTPS 连接,必须存在 SSL 证书和传输配置,否则您的设备将不知道如何加密,更重要的是 信任 另一端.
简而言之,请查看 Network Security Configuration 官方文档以了解这一点及其含义。
如果您现在关心的只是调试,那么您还可以在same page中找到有关如何“绕过”调试构建的信息。
简而言之(我引用):
When debugging an app that connects over HTTPS, you may want to connect to a local development server, which does not have the SSL certificate for your production server.
- 在
res/xml/network_security_config.xml
中添加一个文件(创建它)
- 粘贴类似这样的东西(但是阅读它的意思并理解它,如果你不学习这些东西,你的应用程序和用户信息的安全性就会受到威胁) :
<?xml version="1.0" encoding="utf-8"?>
<network-security-config>
<base-config>
<trust-anchors>
<certificates src="system" />
</trust-anchors>
</base-config>
<debug-overrides>
<trust-anchors>
<certificates src="user" />
</trust-anchors>
</debug-overrides>
</network-security-config>
目前我的 httpclient
只用于 http requests
,但现在我也想使用 https
。
我将 http URL
更改为 https URL
,但我的应用程序无法连接到 url。
仅供参考:我可以用我的浏览器连接到 https url
并得到我的响应,只是不在我的应用程序本身
有什么想法可能是什么问题或我做错了什么吗?
OkHttpClient okHttpClient = new OkHttpClient.Builder()
.connectTimeout(4, TimeUnit.SECONDS)
.build();
retrofit = new Retrofit.Builder()
.baseUrl("https:xyz.com/api/")
.addConverterFactory(GsonConverterFactory.create())
.client(okHttpClient)
.build();
为了能够执行 HTTPS 连接,必须存在 SSL 证书和传输配置,否则您的设备将不知道如何加密,更重要的是 信任 另一端.
简而言之,请查看 Network Security Configuration 官方文档以了解这一点及其含义。
如果您现在关心的只是调试,那么您还可以在same page中找到有关如何“绕过”调试构建的信息。
简而言之(我引用):
When debugging an app that connects over HTTPS, you may want to connect to a local development server, which does not have the SSL certificate for your production server.
- 在
res/xml/network_security_config.xml
中添加一个文件(创建它) - 粘贴类似这样的东西(但是阅读它的意思并理解它,如果你不学习这些东西,你的应用程序和用户信息的安全性就会受到威胁) :
<?xml version="1.0" encoding="utf-8"?>
<network-security-config>
<base-config>
<trust-anchors>
<certificates src="system" />
</trust-anchors>
</base-config>
<debug-overrides>
<trust-anchors>
<certificates src="user" />
</trust-anchors>
</debug-overrides>
</network-security-config>