{"id":1639,"date":"2014-10-20T17:16:20","date_gmt":"2014-10-20T09:16:20","guid":{"rendered":"http:\/\/learn-house.idv.tw\/?p=1639"},"modified":"2014-10-20T23:37:45","modified_gmt":"2014-10-20T15:37:45","slug":"androidhttpsurlconnection%e5%af%a6%e4%bd%9chttps%e8%88%87%e5%bc%b7%e5%88%b6%e5%85%81%e8%a8%b1%e8%87%aa%e7%b0%bd%e6%86%91%e8%ad%89self-signed-certificate","status":"publish","type":"post","link":"https:\/\/learn-house.idv.tw\/?p=1639","title":{"rendered":"[Android]HttpsURLConnection\u5be6\u4f5cHTTPs\u8207\u5f37\u5236\u5141\u8a31\u81ea\u7c3d\u6191\u8b49(Self-Signed Certificate)"},"content":{"rendered":"<p>\u7e7c\u4e0a\u6b21<a title=\"Permanent Link to Apache\u4f3a\u670d\u5668\u6191\u8b49\u7533\u8acb\u8207\u5b89\u88dd\" rel=\"bookmark\" href=\"http:\/\/learn-house.idv.tw\/?p=1589\">Apache\u4f3a\u670d\u5668\u6191\u8b49\u7533\u8acb\u8207\u5b89\u88dd<\/a>\u5f8c\uff0c\u5c31\u53ef\u4ee5\u958b\u59cbimplement\u4e86<\/p>\n<p>\u9019\u6642\u4f60\u7684Web Server\u5df2\u7d93\u6709\u7d93\u904eCA\u8a8d\u53ef\u7684\u5408\u6cd5\u6191\u8b49\uff0c\u4e14\u662f\u5728\u6709\u6548\u7684\u6548\u671f\u5167\uff0c\u56e0\u6b64\u53ef\u4ee5\u76f4\u63a5\u53c3\u8003<a href=\"https:\/\/developer.android.com\/training\/articles\/security-ssl.html\" target=\"_blank\">Security with HTTPS and SSL<\/a><\/p>\n<p>\u5beb\u5b8c\u4f60\u7684APP\u5c31\u53ef\u4ee5\u900f\u904ehttps\u548c\u4f60\u7684Server\u6e9d\u901a\u4e86......\u597d\u8b1b\u89e3\u5b8c\u7562\uff0c\u4f60\u5df2\u7d93\u53ef\u4ee5\u548cServer\u7528https\u9023\u7dda\u4e86<\/p>\n<p>.<br \/>\n.<br \/>\n.<\/p>\n<p>\u4e0d\u904e\u9019\u88e1\u7576\u7136\u4e0d\u6703\u8b1b\u4e00\u822c\u6b63\u5e38\u7684\u60c5\u6cc1\u4e0b\uff0c\u800c\u662f\u7576\u4f60\u6c92\u6709\u9322\u8cb7\u6191\u8b49\u7684\u6642\u5f8c\uff0c\u537b\u53c8\u60f3\u8981\u4f60\u7684app\u80fd\u900f\u904ehttps\u548c\u4f60\u7684server\u6e9d\u901a<\/p>\n<p>\u9019\u6642\u5f8c\u53c8\u8a72\u600e\u9ebc\u505a\u5462???<!--more--><\/p>\n<p>\u901a\u5e38\u4e0d\u7528\u9322\u7684\u6191\u8b49\u5c31\u53ea\u6709\u81ea\u5df1\u7576CA\uff0c\u81ea\u5df1\u7522\u751f\u81ea\u7c3d\u6191\u8b49(self-signed certificate)\u4e00\u9014\u4e86<\/p>\n<p>\u800c\u9019\u7a2e\u60c5\u6cc1\u6709\u5169\u7a2e\u505a\u6cd5\u4f86\u8b93\u4f60\u7684APP\u4fe1\u4efb\u81ea\u7c3d\u6191\u8b49\uff0c\u4e00\u7a2e\u662f\u6bd4\u8f03\u5371\u96aa\uff0c\u53e6\u5916\u4e00\u7a2e\u662f\u5371\u96aa\u4e2d\u6bd4\u8f03\u5b89\u5168\u4e00\u9ede\u7684<\/p>\n<p>\u70ba\u4f55\u5169\u500b\u90fd\u8aaa\u5371\u96aa\u5462? \u56e0\u70ba\u4fe1\u4efb\u975eCA\u8a8d\u53ef\u904e\u7684\u6191\u8b49\u672c\u8eab\u5c31\u662f\u5f88\u5371\u96aa\u7684\u4e00\u4ef6\u4e8b\u4e86<\/p>\n<p>\u4f46\u96d6\u5982\u6b64\u9084\u662f\u6703\u6709\u60c5\u6cc1\u4e0b\u662f\u9700\u8981\u5f37\u5236\u5141\u8a31\u7684 (\u672c\u4eba\u5c31\u6709\u9019\u500b\u9700\u6c42)<\/p>\n<p>\u800c\u7b2c\u4e00\u7a2e\u5371\u96aa\u4e2d\u4f3c\u4e4e\u6bd4\u8f03\u5b89\u5168\u4e00\u9ede\u7684\u4f5c\u6cd5\uff0c\u6211\u6c92\u7814\u7a76\uff0c\u56e0\u70ba\u6211\u89ba\u5f97\u5f88\u300c\u8cbb\u6c23\u300d<\/p>\n<p>\u5b83\u7684\u505a\u6cd5\u662f\u628a\u4f60\u8a8d\u70ba\u5408\u6cd5\u7684\u6191\u8b49\uff0c\u9810\u5148\u5148\u653e\u5230\u4f60\u7684APP\u88e1\uff0c\u7136\u5f8c\u9023\u7dda\u7684\u6642\u5f8c\u76f4\u63a5\u62fflocal\u7aef\u7684\u6191\u8b49\u53bb\u9a57<\/p>\n<p>\u6240\u4ee5\u4e0b\u958b\u59cb\u8b1b\u7b2c\u4e8c\u7a2e\u65b9\u6cd5\uff0c\u4e5f\u5c31\u662f<strong>\u5f37\u5236\u5141\u8a31\u6240\u6709\u81ea\u7c3d\u6191\u8b49<\/strong>(\u5f88\u5371\u96aa\uff0c\u8acb\u659f\u914c\u4f7f\u7528)<\/p>\n<p>\u9996\u5148Android\u8981\u900f\u904ehttps\u8207Server\u6e9d\u901a\u6709\u5169\u7a2e\u5beb\u6cd5\uff0c\u4f7f\u7528<a href=\"HttpsURLConnection\" target=\"_blank\">HttpsURLConnection<\/a>\u6216<a href=\"http:\/\/developer.android.com\/reference\/org\/apache\/http\/client\/HttpClient.html\" target=\"_blank\">HttpClient<\/a><\/p>\n<p>HttpsURLConnection\u662f2.3\u4e4b\u5f8c\u624d\u51fa\u73fe\u7684package\uff0c\u4e5f\u662fAndroid Developer\u5efa\u8b70\u7684\u65b0\u7684\u4f7f\u7528\u65b9\u5f0f<\/p>\n<p>\u66fe\u6709\u770b\u904e\u4e00\u7bc7google\u88e1\u9762\u5de5\u7a0b\u5e2b\u7684\u4e00\u7bc7\u6587\u7ae0\uff0c\u4ed6\u63d0\u5230\u820a\u6709\u7684HttpClient\u7531\u65bc\u63d0\u4f9b\u5f88\u591a\u5f88\u7d30\u7684API<\/p>\n<p>\u6bcf\u6b21Upgrade\u7684\u6642\u5f8c\u90fd\u8981\u8cbb\u5f88\u591a\u5de5\u4f86\u7dad\u8b77\uff0c\u800cHttpsURLConnection\u5df2\u7d93\u63d0\u4f9b\u5927\u90e8\u5206\u4e00\u822cProgramer\u6703\u7528\u5230\u7684API<\/p>\n<p>\u56e0\u6b64\u6f38\u6f38\u7684\u4ed6\u5011\u60f3\u8981\u7528HttpsURLConnection\u4f86\u53d6\u4ee3HttpClient<\/p>\n<p>\u6240\u4ee5\u672c\u4f86\u6211\u4e5f\u60f3\u7528HttpsURLConnection\u4f86\u5be6\u4f5cupload\uff0c\u4f46\u5f8c\u4f86\u767c\u73fe\u6211\u6c92\u8fa6\u6cd5\u53ca\u6642\u7684\u53d6\u5f97\u76ee\u524d\u6a94\u6848\u4e0a\u50b3\u5e7e\u500bbyte~<\/p>\n<p>\u56e0\u70ba\u4e0a\u50b3\u7684\u9019\u500b\u52d5\u4f5c\u5e95\u5c64\u90fd\u5e6b\u6211\u505a\u6389\u4e86\uff0c\u6211\u53ea\u80fd\u628a\u8981\u4e0a\u50b3\u7684\u8cc7\u6599\u585e\u7d66API\uff0c\u4e4b\u5f8c\u5c31\u4e0d\u6b78\u6211\u7ba1\u4e00\u76f4\u7b49\u5230Server\u56de\u61c9\u6210\u529f\u6216\u5931\u6557<\/p>\n<p>\u4f46\u4e5f\u8a31\u662f\u6211\u9084\u6c92\u627e\u5230\u65b9\u6cd5\u53d6\u5f97\u53ca\u6642\u7684\u4e0a\u50b3API\uff0c\u6709\u77e5\u9053\u65b9\u6cd5\u7684\u7db2\u53cb\u518d\u8acb\u6307\u6559\u5206\u4eab\u4e00\u4e0b\u56c9~<\/p>\n<p>\u4ee5\u4e0b\u9084\u662f\u8981\u5148\u8aaa\u660e\u4f7f\u7528HttpsURLConnection\u8981\u5982\u4f55\u624d\u80fd<strong>\u5f37\u5236\u5141\u8a31\u6240\u6709\u81ea\u7c3d\u6191\u8b49<\/strong>\uff0c\u81f3\u65bc<a href=\"http:\/\/learn-house.idv.tw\/?p=1659\" target=\"_blank\">HttpClient<\/a>\u6703\u518d\u53e6\u958b\u4e00\u7bc7\u89e3\u8aaa<\/p>\n<p>\u6211\u60f3\uff0c\u958b\u59cb\u8aaaHttpsURLConnection\u524d\u4e5f\u4e0d\u7528\u89e3\u91cb\u592a\u591a\uff0c\u76f4\u63a5\u770bCode\u5427\uff1a<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\n\t\ttry{\r\n\t\t\tURL url = new URL(&quot;https:\/\/127.0.0.1\/weblogin.php&quot;);\r\n\t\t    HttpsURLConnection conn = (HttpsURLConnection) url.openConnection();\r\n\t\t\t\/\/AllowAllHostNameVerifier\r\n\t\t    HttpsURLConnection.setDefaultHostnameVerifier(new AllowAllHostNameVerifier());\r\n\r\n\t\t    \/\/ Create the SSL connection\r\n\t\t    SSLContext sc;\r\n\t\t    sc = SSLContext.getInstance(&quot;TLS&quot;);\r\n\t\t    \/\/The following are the most important to allow self-signed certificate\r\n\t\t    sc.init(null, new TrustManager&#x5B;] {\r\n\t\t    \t\t  new X509TrustManager() {\r\n\t\t    \t\t\t    public void checkClientTrusted(X509Certificate&#x5B;] chain, String authType) {}\r\n\t\t    \t\t\t    public void checkServerTrusted(X509Certificate&#x5B;] chain, String authType) {}\r\n\t\t    \t\t\t    public X509Certificate&#x5B;] getAcceptedIssuers() { return new X509Certificate&#x5B;]{}; }\r\n\t\t    \t\t\t  }\r\n\t\t    \t\t\t}, null);\r\n\t\t    conn.setSSLSocketFactory(sc.getSocketFactory());\r\n\r\n\t\t    conn.setDoOutput(true);\r\n\t\t    conn.setDoInput(true);\r\n\t\t    conn.setRequestMethod(&quot;POST&quot;);\r\n\t\t    \/\/ set Timeout and method\r\n\t\t    conn.setReadTimeout(20000);\r\n\t\t    conn.setConnectTimeout(7000);\r\n\r\n\t\t    conn.setRequestProperty(&quot;Content-Length&quot;, String.valueOf(args.getBytes().length));\r\n\t\t    java.io.DataOutputStream dos = new java.io.DataOutputStream(conn.getOutputStream());\r\n\t\t    dos.writeBytes(args);\r\n\r\n\t\t    java.io.BufferedReader rd = new java.io.BufferedReader(new java.io.InputStreamReader(conn.getInputStream(), &quot;utf-8&quot;));\r\n\t\t    String line;\r\n\t\t    while ((line = rd.readLine()) != null) {\r\n\t\t        content.append(line);\r\n\t\t    }\r\n\t\t\trd.close();\r\n\r\n\t\t\tresponseContent = content.toString();\r\n\t\t\tSystem.out.println(responseContent);\r\n\r\n\t\t} catch (Exception e) {\r\n\t\t\te.printStackTrace();\r\n\t\t\ttv.setText(e.toString());\r\n\t\t}\r\n\r\n<\/pre>\n<p>\u4e0b\u9762\u5247\u662f\u5be6\u4f5c\u7528\u4f86\u9a19APP\u8aaaServer\u7aef\u9a57\u8b49\u6c92\u554f\u984c\u7684Class<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\n\tpublic class AllowAllHostNameVerifier implements HostnameVerifier {\r\n\r\n\t    public boolean verify(String hostname, SSLSession session) {\r\n\t        \/\/Log.i(&quot;RestUtilImpl&quot;, &quot;Approving certificate for &quot; + hostname);\r\n\t    \tSystem.out.println(&quot;RestUtilImpl Approving certificate for &quot; + hostname);\r\n\t        return true;\r\n\t    }\r\n\t}\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u7e7c\u4e0a\u6b21Apache\u4f3a\u670d\u5668\u6191\u8b49\u7533\u8acb\u8207\u5b89\u88dd\u5f8c\uff0c\u5c31\u53ef\u4ee5\u958b\u59cbimplement\u4e86 \u9019\u6642\u4f60\u7684Web Server\u5df2\u7d93\u6709\u7d93 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5],"tags":[],"class_list":["post-1639","post","type-post","status-publish","format-standard","hentry","category-5"],"_links":{"self":[{"href":"https:\/\/learn-house.idv.tw\/index.php?rest_route=\/wp\/v2\/posts\/1639"}],"collection":[{"href":"https:\/\/learn-house.idv.tw\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/learn-house.idv.tw\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/learn-house.idv.tw\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/learn-house.idv.tw\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1639"}],"version-history":[{"count":0,"href":"https:\/\/learn-house.idv.tw\/index.php?rest_route=\/wp\/v2\/posts\/1639\/revisions"}],"wp:attachment":[{"href":"https:\/\/learn-house.idv.tw\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1639"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/learn-house.idv.tw\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1639"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/learn-house.idv.tw\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1639"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}