顯示具有 WebService 標籤的文章。 顯示所有文章
顯示具有 WebService 標籤的文章。 顯示所有文章

2013年1月17日 星期四

使用AXIS2建立WebService Client

 Axis2 是一個WebService的核心支援引擎。 AXIS2對舊有的AXIS重新設計及重寫,並提供兩種語言Java 及 C 的開發版本,這裡介紹使用Eclipse配合AXIS2建立WebService Client專案 。

2013年1月16日 星期三

AXIS2 WebService Client存取Server憑證過期解決方法

當Server端的憑證過期,無法直接用Eclipse直接連接https的URL時,可利用瀏覽器將.wsdl檔下載至Local端,再將檔案匯入Eclipse中,使用axis2建立一個WebServiceClient的project,詳細建立步驟

由於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了。