{"id":169,"date":"2023-05-18T12:05:30","date_gmt":"2023-05-18T04:05:30","guid":{"rendered":"https:\/\/app.60day.cn\/appday\/?p=169"},"modified":"2023-05-18T12:05:30","modified_gmt":"2023-05-18T04:05:30","slug":"android-%e6%89%93%e5%8c%85%e5%bf%bd%e7%95%a5https%e6%a0%a1%e9%aa%8c%e9%9c%80%e8%a6%81%e6%80%8e%e4%b9%88%e5%81%9a%ef%bc%9f","status":"publish","type":"post","link":"https:\/\/app.60day.cn\/appday\/kaifaapp\/169\/","title":{"rendered":"android \u6253\u5305\u5ffd\u7565https\u6821\u9a8c\u9700\u8981\u600e\u4e48\u505a\uff1f"},"content":{"rendered":"<p>\u5728Android\u5e94\u7528\u5f00\u53d1\u8fc7\u7a0b\u4e2d\uff0c\u6211\u4eec\u7ecf\u5e38\u9700\u8981\u4f7f\u7528HTTPS\u534f\u8bae\u6765\u4fdd\u8bc1\u6570\u636e\u7684\u5b89\u5168\u6027\u3002\u4f46\u6709\u65f6\u5019\uff0c\u6211\u4eec\u9700\u8981\u5728\u6d4b\u8bd5\u9636\u6bb5\u5ffd\u7565HTTPS\u7684\u6821\u9a8c\u3002\u672c\u6587\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u5728Android\u5e94\u7528\u4e2d\u5ffd\u7565HTTPS\u6821\u9a8c\u3002<\/p>\n<p>HTTPS\u6821\u9a8c\u662f\u4ec0\u4e48\uff1f<\/p>\n<p>HTTPS\u662fHTTP\u7684\u52a0\u5bc6\u7248\u672c\uff0c\u4e3a\u4e86\u4fdd\u8bc1\u6570\u636e\u7684\u5b89\u5168\u6027\uff0cHTTPS\u5728\u4f20\u8f93\u6570\u636e\u65f6\u4f1a\u4f7f\u7528SSL\/TLS\u534f\u8bae\u8fdb\u884c\u52a0\u5bc6\u3002\u5728SSL\/TLS\u534f\u8bae\u4e2d\uff0c\u4f1a\u5bf9\u670d\u52a1\u5668\u7aef\u7684\u8bc1\u4e66\u8fdb\u884c\u6821\u9a8c\uff0c\u786e\u4fdd\u901a\u4fe1\u7684\u5b89\u5168\u6027\u3002\u5982\u679c\u8bc1\u4e66\u6821\u9a8c\u5931\u8d25\uff0cHTTPS\u901a\u4fe1\u5c31\u4f1a\u5931\u8d25\u3002<\/p>\n<p>\u4e3a\u4ec0\u4e48\u9700\u8981\u5ffd\u7565HTTPS\u6821\u9a8c\uff1f<\/p>\n<p>\u5728\u6d4b\u8bd5\u9636\u6bb5\uff0c\u6211\u4eec\u53ef\u80fd\u9700\u8981\u4f7f\u7528\u4e00\u4e9b\u81ea\u7b7e\u540d\u7684\u8bc1\u4e66\u6216\u8005\u4e0d\u53d7\u4fe1\u4efb\u7684\u8bc1\u4e66\uff0c\u6b64\u65f6\u5982\u679c\u8fdb\u884cHTTPS\u901a\u4fe1\uff0c\u5c31\u4f1a\u56e0\u4e3a\u8bc1\u4e66\u6821\u9a8c\u5931\u8d25\u800c\u65e0\u6cd5\u901a\u4fe1\u3002\u56e0\u6b64\uff0c\u6211\u4eec\u9700\u8981\u5728\u6d4b\u8bd5\u9636\u6bb5\u5ffd\u7565HTTPS\u7684\u6821\u9a8c\u3002<\/p>\n<p>\u5ffd\u7565HTTPS\u6821\u9a8c\u7684\u539f\u7406<\/p>\n<p>\u5ffd\u7565HTTPS\u6821\u9a8c\u7684\u539f\u7406\u662f\u901a\u8fc7\u81ea\u5b9a\u4e49\u4e00\u4e2aTrustManager\u6765\u5b9e\u73b0\u7684\u3002\u5728\u6b63\u5e38\u60c5\u51b5\u4e0b\uff0cAndroid\u7cfb\u7edf\u4f1a\u4f7f\u7528\u7cfb\u7edf\u9ed8\u8ba4\u7684TrustManager\u6765\u8fdb\u884c\u8bc1\u4e66\u6821\u9a8c\u3002\u800c\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u81ea\u5b9a\u4e49\u4e00\u4e2aTrustManager\uff0c\u4f7f\u5176\u4e0d\u8fdb\u884c\u8bc1\u4e66\u6821\u9a8c\uff0c\u4ece\u800c\u8fbe\u5230\u5ffd\u7565HTTPS\u6821\u9a8c\u7684\u76ee\u7684\u3002<\/p>\n<p>\u5ffd\u7565HTTPS\u6821\u9a8c\u7684\u8be6\u7ec6\u6b65\u9aa4<\/p>\n<p>1. \u7f16\u5199\u4e00\u4e2a\u81ea\u5b9a\u4e49\u7684TrustManager\u7c7b\uff0c\u91cd\u5199\u5176\u4e2d\u7684checkServerTrusted\u65b9\u6cd5\u3002\u5728\u8be5\u65b9\u6cd5\u4e2d\u4e0d\u505a\u4efb\u4f55\u64cd\u4f5c\uff0c\u76f4\u63a5\u8fd4\u56de\u5373\u53ef\u3002<\/p>\n<p>&#8220;`<\/p>\n<p>public class MyTrustManager implements X509TrustManager {<\/p>\n<p>    @Override<\/p>\n<p>    public void checkClientTrusted(X509Certificate[] chain, String authType) throws CertificateException {<\/p>\n<p>    }<\/p>\n<p>    @Override<\/p>\n<p>    public void checkServerTrusted(X509Certificate[] chain, String authType) throws CertificateException {<\/p>\n<p>    }<\/p>\n<p>    @Override<\/p>\n<p>    public X509Certificate[] getAcceptedIssuers() {<\/p>\n<p>        return new X509Certificate[0];<\/p>\n<p>    }<\/p>\n<p>}<\/p>\n<p>&#8220;`<\/p>\n<p>2. \u5728HttpURLConnection\u6216\u8005OkHttpClient\u4e2d\u8bbe\u7f6eTrustManager\u3002\u5982\u679c\u4f7f\u7528\u7684\u662fHttpURLConnection\uff0c\u53ef\u4ee5\u901a\u8fc7\u8c03\u7528setDefaultSSLSocketFactory\u65b9\u6cd5\u6765\u8bbe\u7f6eTrustManager\u3002\u5982\u679c\u4f7f\u7528\u7684\u662fOkHttpClient\uff0c\u5219<a href=\"https:\/\/www.yimenapp.net\/dabao-2840.html\">win apk\u6253\u5305\u8f6f\u4ef6<\/a>\u9700\u8981\u901a\u8fc7\u8bbe\u7f6eSSLSocketFactory\u6765\u8bbe\u7f6eTrustManager\u3002<\/p>\n<p><a href=\"https:\/\/www.yimenapp.net\/dabao-2611.html\">ios applog\u5728\u7ebf\u751f\u6210<\/a><\/p>\n<p>&#8220;`<\/p>\n<p>\/\/ \u4f7f\u7528HttpURLConnection\u7684\u793a\u4f8b\u4ee3\u7801<\/p>\n<p>SSLContext sslContext = SSLContext.getInstance(&#8220;TLS&#8221;);<\/p>\n<p>sslContext.init(null, <\/p>\n<p><figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/i.cdn.yimenapp.com\/2023-03\/31\/16\/29731292916.jpg\" \/><\/figure>\n<\/p>\n<p>new TrustManager[]{new MyTrustManager()}, new SecureRandom());<\/p>\n<p>HttpsURLConnection.setDefaultSSLSocketFactory(sslContext.getSocketFactory());<\/p>\n<p>\/\/ \u4f7f\u7528OkHttpClient\u7684\u793a\u4f8b\u4ee3\u7801<\/p>\n<p>OkHttpClient.Builder builder = new OkHttpClient.Builder();<\/p>\n<p>SSLContext sslContext = SSLContext.getInstance(&#8220;TLS&#8221;);<\/p>\n<p>sslContext.init(null, new TrustManager[]{new MyTrustManager()}, new SecureRandom());<\/p>\n<p>builder.sslSocketFactory(sslContext.getSocketFactory(), new MyTrustManager());<\/p>\n<p>OkHttpClient client = builder.build();<\/p>\n<p>&#8220;`<\/p>\n<p>3. \u73b0\u5728\uff0c\u6211\u4eec\u5c31\u53ef\u4ee5\u5728\u6d4b\u8bd5\u73af\u5883\u4e2d\u4f7f\u7528\u81ea\u7b7e\u540d\u7684\u8bc1\u4e66\u6216\u8005\u4e0d\u53d7\u4fe1\u4efb\u7684\u8bc1\u4e66\u8fdb\u884cHTTPS\u901a\u4fe1\u4e86\u3002<\/p>\n<p>\u603b\u7ed3<\/p>\n<p>\u5728\u6d4b\u8bd5\u9636\u6bb5\uff0c\u6211\u4eec\u53ef\u80fd\u9700\u8981\u5ffd\u7565HTTPS\u7684\u6821\u9a8c\u3002\u8fd9\u65f6\uff0c\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u81ea\u5b9a\u4e49\u4e00\u4e2aTrustManager\u6765\u5b9e\u73b0\u3002\u5728\u81ea\u5b9a\u4e49\u7684TrustManager\u4e2d\uff0c\u6211\u4eec\u53ef\u4ee5\u4e0d\u8fdb\u884c\u8bc1\u4e66\u6821\u9a8c\uff0c\u4ece\u800c\u8fbe\u5230\u5ffd\u7565HTTPS\u6821\u9a8c\u7684\u76ee\u7684\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5728Android\u5e94\u7528\u5f00\u53d1\u8fc7\u7a0b\u4e2d\uff0c\u6211\u4eec\u7ecf\u5e38\u9700\u8981\u4f7f\u7528HTTPS\u534f\u8bae\u6765\u4fdd\u8bc1\u6570\u636e\u7684\u5b89\u5168\u6027\u3002\u4f46\u6709\u65f6\u5019\uff0c\u6211\u4eec\u9700\u8981\u5728\u6d4b\u8bd5\u9636\u6bb5\u5ffd\u7565HTTPS\u7684\u6821\u9a8c\u3002\u672c\u6587\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u5728Android\u5e94\u7528\u4e2d\u5ffd\u7565HTTPS\u6821\u9a8c\u3002HTT<\/p>\n","protected":false},"author":17,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[425,427,426,16,240],"topic":[],"class_list":["post-169","post","type-post","status-publish","format-standard","hentry","category-kaifaapp","tag-cordoveapk","tag-app","tag-426","tag-16","tag-240"],"_links":{"self":[{"href":"https:\/\/app.60day.cn\/appday\/wp-json\/wp\/v2\/posts\/169","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/app.60day.cn\/appday\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/app.60day.cn\/appday\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/app.60day.cn\/appday\/wp-json\/wp\/v2\/users\/17"}],"replies":[{"embeddable":true,"href":"https:\/\/app.60day.cn\/appday\/wp-json\/wp\/v2\/comments?post=169"}],"version-history":[{"count":0,"href":"https:\/\/app.60day.cn\/appday\/wp-json\/wp\/v2\/posts\/169\/revisions"}],"wp:attachment":[{"href":"https:\/\/app.60day.cn\/appday\/wp-json\/wp\/v2\/media?parent=169"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/app.60day.cn\/appday\/wp-json\/wp\/v2\/categories?post=169"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/app.60day.cn\/appday\/wp-json\/wp\/v2\/tags?post=169"},{"taxonomy":"topic","embeddable":true,"href":"https:\/\/app.60day.cn\/appday\/wp-json\/wp\/v2\/topic?post=169"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}