首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > 编程 >

httpclient访问https服务,可以信赖证书

2012-09-09 
httpclient访问https服务,可以信任证书private HttpClient initHttpClient() {if(httpclient null){try

httpclient访问https服务,可以信任证书

private HttpClient initHttpClient() {if(httpclient == null){try {X509TrustManager tm = new X509TrustManager() {public void checkClientTrusted(X509Certificate[] arg0,String arg1) throws CertificateException {}public void checkServerTrusted(X509Certificate[] arg0,String arg1) throws CertificateException {}public X509Certificate[] getAcceptedIssuers() {return null;}};SSLContext sslcontext = SSLContext.getInstance("TLS");sslcontext.init(null, new TrustManager[] { tm }, null);SSLSocketFactory ssf = new SSLSocketFactory(sslcontext);ClientConnectionManager ccm = new DefaultHttpClient().getConnectionManager();SchemeRegistry sr = ccm.getSchemeRegistry();sr.register(new Scheme("https", 8443, ssf));HttpParams params = new BasicHttpParams();params.setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, 1000);params.setParameter(CoreConnectionPNames.SO_TIMEOUT, 5000);httpclient = new DefaultHttpClient(ccm,params);} catch (Exception ex) {ex.printStackTrace();}}return httpclient;} 

?

热点排行