兩年半前的Vibo案,Aqual Logic竟無法註冊.Net的WebServices,用Axis去call .net web services完全不通。近日受命研究連接某公司的.net web services,要用Java去call,結果在JBoss的官方文件竟找到Solution,出處在http://docs.jboss.org/jbossas/jboss4guide/r2/html/ch12.html,還是用javax.xml.rpc套件去call的,反而我還是無法使用Axis踹出來,不免有種滄海桑田之感。

  某公司的web services的URL是https://…/Security.asmx。以asmx結尾是.Net手路之一,所以後面加?wsdl確定有它的存在。在該網頁檢視發現有個URL地位很重要:http://tempuri.org/,這麼解釋它:它是.Net WebServices參考的Namespace(命名空間)。之後找到JBoss附的source如下:

import javax.xml.rpc.Service;
import javax.xml.rpc.ServiceFactory;
import javax.xml.rpc.Call;

import javax.xml.namespace.QName;

import java.net.URL;
public class WsClient {
    public static void main(String[] args) throws Exception {
        String urlstr   = "
https://…/Security.asmx?wsdl";  // 後面加?wsdl
        URL url =  new URL(urlstr);

        String ns        = "http://tempuri.org/";
        QName  qname     = new QName(ns, "Security");      // 查wsdl檔的<wsdl:service>的name屬性
        QName  port      = new QName(ns, "SecuritySoap");  //
查wsdl檔的<wsdl:portType>的name屬性 
        QName  operation = new QName(ns, "getConnection"); //
查wsdl檔的<wsdl:operation>的name屬性

        ServiceFactory factory = ServiceFactory.newInstance();
        Service        service = factory.createService(url, qname);
        Call           call    = service.createCall(port, operation);

        System.out.println("output:" + call.invoke(new Object[] {"UserID", "Password"}));
    }
}

  結果真的如該公司文件所寫,傳回XML String。其實傳XML String是比較不費工的作法,Web Services是可以傳回不少種類的型態,包括Collection,但要去接收就粉累人了。WSDL裡的service name相當於一個Jar、portType name相當於一個Interface、而operation name則相當於interface裡的method。

  後來找出用Axis的call法,才發現Axis也是要照上面方式寫,Axis的Service、Call類別其實都自javax.xml.rpc套件演變而來,卻沒辦法用Axis標準寫法,先宣告Service和Call再去set一些屬性,而是和上面一樣全用建構子傳參數。如下:

import java.net.URL;

import javax.xml.namespace.QName;

import org.apache.axis.client.Call;
import org.apache.axis.client.Service;

public class DotNetWebService {
    public static void main(String[] args) throws Exception { 
        String method = "getConnection";
        Service service = new Service(
                new URL("
https://…/Security.asmx?wsdl"),
                new QName("
http://tempuri.org/", "Security"));
        String ns        = "
http://tempuri.org/";
        QName  port      = new QName(ns, "SecuritySoap");
        QName  operation = new QName(ns, "getConnection");
        Call call = (Call) service.createCall(port, operation); 
        String ret = (String)call.invoke(new String[]{
{"UserID", "Password" });
        System.out.println(ret);
    } 
}

  寫到此處,忽然很想用力的丟滑鼠,這不是早該兩年前就該踹出來的嗎?EBS不給時間研發,再來批評軟體開發中心技術水平差。全面採取保守不冒風險的策略,其實就是置專案於最大的風險,而和Vibo全面對幹,卻又是採取太過激進、甘冒大不諱的策略。雖然EBS裡主導這種爛策略的龜sons都被fire,但其性若不改,其作為適足讓台灣資訊業蒙羞矣!

arrow
arrow
    全站熱搜
    創作者介紹
    創作者 Jemmy 的頭像
    Jemmy

    Jemmy Walker

    Jemmy 發表在 痞客邦 留言(0) 人氣()