1. 撰寫.NET版 Web Services

首先,啟動 Microsoft Visual Studio 2005 
File → New → Web site

選 ASP.NET Web Service

開啟 Service.cs

然後開始寫程式,就完成 Web Service 的建立了!

因為WEB SERVICE 主打的就是一個共通的規格,任何語言只要遵照這個規格就可以互相使用。

2. 利用Eclipse建立Web Sercvices Client端

先叫出.NET WEB SERVICE的WSDL,然後在Eclipse上New一個Web Service Client

接著輸入剛剛的WSDL網址,Eclipse很順利的產生兩個JAVA檔

ServiceCallbackHandler.java
ServiceStub.java

由於寫的是一個,丟一個字串進去,會把我字串在回覆回來的 HelloWorld 程式
所以我就跟上次一樣,仿照同樣的方式寫一個程式來呼叫.NET WEB SERVICE


port org.tempuri.ServiceStub;
import org.tempuri.ServiceStub.HelloWorld;
import org.tempuri.ServiceStub.HelloWorldResponse;

public class testClient {

public static void main(String[] args) throws Exception {

    ServiceStub cs = new ServiceStub();
    HelloWorld csRequest = new HelloWorld();
    csRequest.setStr(".Net Web Service test");
    HelloWorldResponse csResponse = cs.HelloWorld(csRequest);
    System.out.println(".Net Response : " + csResponse.getHelloWorldResult());
  }
}


寫好以後~~ RUN!

跳出一個錯誤訊息
"Exception in thread "main" org.apache.axis2.AxisFault: The input stream for an incoming message is null."

囧... 是誰說WEB SERVICE都一樣的...

在拜過Google大神後,大神給我了一個提示...

在New物件的時候,要在建構子裡帶上URI的參數阿...
於是需要改寫了一下程式...

2008.12.16 14:59 更新:
不加URI也是可以的,建構子會帶入預設值,所以關鍵點應該在稍後提到的那一長串程式碼。

import org.tempuri.ServiceStub;
import org.tempuri.ServiceStub.HelloWorld;
import org.tempuri.ServiceStub.HelloWorldResponse;

public class testClient {

public static void main(String[] args) throws Exception {

      String uri = "http://localhost:3514/ws/Service.asmx";
      ServiceStub cs = new ServiceStub(uri);
      HelloWorld csRequest = new HelloWorld();
      csRequest.setStr(".Net Web Service test");
      HelloWorldResponse csResponse = cs.HelloWorld(csRequest);
       System.out.println(".Net Response : " + csResponse.getHelloWorldResult());
    }
}


再執行一次,RUN!

BeepBeep!Eclispe 又一個錯誤訊息!
"Exception in thread "main" org.apache.axis2.AxisFault: Software caused connection abort: recv failed"

需要再度改寫程式

import org.tempuri.ServiceStub;
import org.tempuri.ServiceStub.HelloWorld;
import org.tempuri.ServiceStub.HelloWorldResponse;

public class testClient {

public static void main(String[] args) throws Exception {

      String uri = "http://localhost:3514/ws/Service.asmx";
      ServiceStub cs = new ServiceStub(uri);
      cs._getServiceClient().getOptions().setProperty            (org.apache.axis2.transport.http.HTTPConstants.CHUNKED, Boolean.FALSE);
      HelloWorld csRequest = new HelloWorld();
      csRequest.setStr(".Net Web Service test");
      HelloWorldResponse csResponse = cs.HelloWorld(csRequest);
      System.out.println(".Net Response : " + csResponse.getHelloWorldResult());
  }
}

Google查來的資料說:
注意這一句!
cs._getServiceClient().getOptions().setProperty(org.apache.axis2.transport.http.HTTPConstants.CHUNKED, Boolean.FALSE);

不做這個設置,一開始我怎麼都調用失敗,Axis2報告說HTTP send recv出錯(記不清楚錯誤信息了)。用tcpmon查看調用服務時的tcp傳輸,發現.net的web server根本不接受axis2的soap包。(btw,tcpmon是個不錯的工具啊。)

google了很久發現原因是,axis2在做http傳輸時採用了「chunked」模式,而.net的web server不支持。

「axis中使用的是HTTP/1.0協議,而.NET和axis2使用的是HTTP/1.1協議,後兩者的區別在於.NET未使用ns1的命名空間前綴打包SOAP請求,且axis2使用了Content-Encoding: chunked頭。 所以必須在axis2中設置一下。」

 

 

參考資料:
用Java調用.net的Web service ─ 趙翔鵬的Blog - Xiangpeng's Thinkpad
java使用AXIS2調用asp.net的WebService ─ heyanyang的個人空間
參考資料:
用Java調用.net的Web service ─ 趙翔鵬的Blog - Xiangpeng's Thinkpad
java使用AXIS2調用asp.net的WebService ─ heyanyang的個人空間

 

arrow
arrow
    全站熱搜

    白努力電腦日記 發表在 痞客邦 留言(0) 人氣()