{"id":660,"date":"2013-08-29T19:00:29","date_gmt":"2013-08-29T11:00:29","guid":{"rendered":"http:\/\/www.yeetrack.com\/?p=660"},"modified":"2013-09-27T22:38:40","modified_gmt":"2013-09-27T14:38:40","slug":"defaulthttpclient-%e6%8a%a5%e9%94%99https-peer-not-authenticated%e8%a7%a3%e5%86%b3","status":"publish","type":"post","link":"https:\/\/www.yeetrack.com\/?p=660","title":{"rendered":"DefaultHttpClient \u62a5\u9519https peer not authenticated\u89e3\u51b3"},"content":{"rendered":"<p>\u4f7f\u7528apache\u7684httpcomponents\u5305\uff0c\u80fd\u591f\u5f88\u65b9\u4fbf\u7684\u4f7f\u7528java\u6765\u53d1\u9001http\u8bf7\u6c42\uff0c\u4e0e\u670d\u52a1\u5668\u4ea4\u4e92\u3002\u5176\u4e2d\u7684DefaultHttpClient\u662f\u6211\u4eec\u53d1\u9001http\u8bf7\u6c42\u7684\u5ba2\u6237\u7aef\u7c7b\uff0c\u5b83\u652f\u6301http\u548chttps\u3002\u5982\u679c\u8981\u4f7f\u7528https\u7684\u8bdd\uff0c\u5728\u6d4b\u8bd5\u73af\u5883\u4e2d\u7ecf\u5e38\u56e0\u4e3a\u8bc1\u4e66\u65e0\u6548\uff0c\u800c\u62a5\u9519Exception in thread \"main\" javax.net.ssl.SSLPeerUnverifiedException: peer not authenticated\u3002\u8fd9\u65f6\u6211\u4eec\u53ef\u4ee5\u6307\u5b9aDefaultHttpClient\u5ffd\u7565\u8bc1\u4e66\u8ba4\u8bc1\u9519\u8bef\uff0c\u7ee7\u7eed\u8fdb\u884chttp\u4ea4\u4e92\uff0c\u4e3b\u8981\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<pre><code>\/**\n * \u53c2\u6570base\u5c31\u662f\u6211\u4eec\u521b\u5efa\u7684DefaultHttpClient\u5bf9\u8c61\n ** \/\npublic static HttpClient wrapClient(HttpClient base) \n{\n    try {\n        SSLContext ctx = SSLContext.getInstance(\"TLS\");<!--more-->\n        X509TrustManager tm = new X509TrustManager() {\n\n            public void checkClientTrusted(X509Certificate[] chain,\n                    String authType) throws CertificateException\n            {\n                \/\/ TODO Auto-generated method stub\n\n            }\n\n            public void checkServerTrusted(X509Certificate[] chain,\n                    String authType) throws CertificateException\n            {\n                \/\/ TODO Auto-generated method stub\n\n            }\n\n            public X509Certificate[] getAcceptedIssuers()\n            {\n                \/\/ TODO Auto-generated method stub\n                return null;\n            }\n\n        };\n        ctx.init(null, new TrustManager[] { tm }, null);\n        SSLSocketFactory ssf = new SSLSocketFactory(ctx);\n        ssf.setHostnameVerifier(SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);\n        ClientConnectionManager ccm = base.getConnectionManager();\n        SchemeRegistry sr = ccm.getSchemeRegistry();\n        \/\/\u8bbe\u7f6e\u8981\u4f7f\u7528\u7684\u7aef\u53e3\uff0c\u9ed8\u8ba4\u662f443\n        sr.register(new Scheme(\"https\", ssf, 443));\n        return new DefaultHttpClient(ccm, base.getParams());\n    } catch (Exception ex) {\n        ex.printStackTrace();\n        return null;\n    }\n}\n<\/code><\/pre>\n<p>\u5728\u4f7f\u7528\u65f6\uff0c\u76f4\u63a5\u8c03\u7528wrapClient()\u65b9\u6cd5\u5373\u53ef\u3002<\/p>\n<pre><code>DefaultHttpClient httpClient = wrapClient(new DefaultHttpClient());\nHttpGet get = new HttpGet(\"https:\/\/somesite.domain.com\");\nHttpResponse response = httpClient.execute(get);\nString result = EntityUtils.toString(response.getEntity());\nSystem.out.println(resultString);\nhttpClient.getConnectionManager().shutdown();<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u4f7f\u7528apache\u7684httpcomponents\u5305\uff0c\u80fd\u591f\u5f88\u65b9\u4fbf\u7684\u4f7f\u7528java\u6765\u53d1\u9001http\u8bf7\u6c42\uff0c\u4e0e\u670d\u52a1\u5668\u4ea4\u4e92\u3002\u5176\u4e2d\u7684DefaultHttpClient\u662f\u6211\u4eec\u53d1\u9001http\u8bf7\u6c42\u7684\u5ba2\u6237\u7aef\u7c7b\uff0c\u5b83\u652f\u6301http\u548ch&#46;&#46;&#46;<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"pgc_sgb_lightbox_settings":"","footnotes":""},"categories":[33],"tags":[8,7,5],"class_list":["post-660","post","type-post","status-publish","format-standard","hentry","category-coding","tag-java","tag-7","tag-5"],"views":13203,"_links":{"self":[{"href":"https:\/\/www.yeetrack.com\/index.php?rest_route=\/wp\/v2\/posts\/660","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.yeetrack.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.yeetrack.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.yeetrack.com\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.yeetrack.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=660"}],"version-history":[{"count":1,"href":"https:\/\/www.yeetrack.com\/index.php?rest_route=\/wp\/v2\/posts\/660\/revisions"}],"predecessor-version":[{"id":661,"href":"https:\/\/www.yeetrack.com\/index.php?rest_route=\/wp\/v2\/posts\/660\/revisions\/661"}],"wp:attachment":[{"href":"https:\/\/www.yeetrack.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=660"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.yeetrack.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=660"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.yeetrack.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=660"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}