Android uses okhttp to request self signed HTTPS websites as an example

preface

Considering the security problem, many companies use HTTPS encryption protocol for data transmission in the project. However, some companies do not want to spend a sum of money to apply for CA certificates, so they use self signed certificates.

Okhttp can access HTTPS links certified by Ca by default. For example, baidu home page is also an HTTPS link( https://www.baidu.com/ )。 However, if the server is self signed by your company (that is, the certificate generated by keytool instead of the certificate certified by CA), okhttp cannot be accessed, such as 12306 website( https://kyfw.12306.cn/otn/ ), the following error will be reported:

How HTTPS works

HTTPS needs a handshake between the client (browser) and the server (website) before transmitting data. In the handshake process, the password information of both parties to encrypt the transmitted data will be established. A brief description of the handshake process is as follows:

If there is any error in the handshake process, the encrypted connection will be disconnected, thus preventing the transmission of private information.

Request self signed HTTPS server data using okhttp

Let's use 12306 website as an example

1. First go to the homepage of 12306 website to download the certificate http://www.12306.cn/

2. Put the downloaded certificate srca.cer in the assets folder of the project.

3. Add HTTPS tool class

4. Request in code

5. Finally, if you can print out these information, it means that the request is successful!

Note: don't forget to add permissions and dependent okhttp libraries

Demo address: https://github.com/Alpha58/okhttps

The above is the whole content of this article. I hope it will be helpful to your study, and I hope you can support programming tips.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>