當Server端的憑證過期,無法直接用Eclipse直接連接https的URL時,可利用瀏覽器將.wsdl檔下載至Local端,再將檔案匯入Eclipse中,使用axis2建立一個WebServiceClient的project,詳細建立步驟。
由於Server憑証過期,Client端要存取Server時會造成錯誤,在axis2中可加入以下程式碼,忽略憑証過期的警告。程式碼如下:
由於Server憑証過期,Client端要存取Server時會造成錯誤,在axis2中可加入以下程式碼,忽略憑証過期的警告。程式碼如下:
public static void trustAllSSL() { TrustManager[] trustAllCerts = new TrustManager[] { 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; } } }; HostnameVerifier hostVerify = new HostnameVerifier() { public boolean verify(String hostname, SSLSession session) { return true; } }; try { SSLContext sc = SSLContext.getInstance("SSL"); sc.init(null, trustAllCerts, new java.security.SecureRandom()); HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory()); HttpsURLConnection.setDefaultHostnameVerifier(hostVerify); } catch (Exception e) { e.printStackTrace(); } }
在Client初始化前執行 trustAllSSL(),就可正常存取WebService了。
沒有留言:
張貼留言