WEB services用戶端例子
說明:本文材料取自Axis user's guide
在Apache的公用Axis伺服器上有一個echoString的服務,你向該服務發一個串,該服務能夠返回同一個串給你.下面我們寫一個WEB services用戶端例子來調用該服務:
1?? import org.apache.axis.client.Call;
2?? import org.apache.axis.client.Service;
3?? import javax.xml.namespace.QName;
4??
5?? public class TestClient {
6????? public static void main(String [] args) {
7????????? try {
8????????????? String endpoint =
9?????????????????????? "http://nagoya.apache.org:5049/axis/services/echo";
10?
?????????????? //建立兩個JAX-RPC對象
11???????????? Service? service = new Service();
12???????????? Call???? call??? = (Call) service.createCall();
13?
?????????????? //SOAP訊息的目的地
14???????????? call.setTargetEndpointAddress( new java.net.URL(endpoint) );
?????????????? //要調用的操作名稱
15???????????? call.setOperationName(new QName("http://soapinterop.org/", "echoString"));
16?
?????????????? //用數組形式傳入參數
17???????????? String ret = (String) call.invoke( new Object[] { "Hello!" } );
18?
19???????????? System.out.println("Sent 'Hello!', got '" + ret + "'");
20???????? } catch (Exception e) {
21???????????? System.err.println(e.toString());
22???????? }
23???? }
24? }
上面的檔案在axis的D:/axis/axis-1_1/samples/userguide/example1目錄下.
加入需要的包,串連上網路,運行程式,你將得到下面的結果:
Sent 'Hello!', got 'Hello!'
用tcpmon或SOAP monitor工具查看傳遞的SOAP訊息,如下:
http://www.w3.org/2001/XMLSchema"
?????????????????? xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"
?????????????????? xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
?
??? http://soapinterop.org/">
????? Hello!
???
?
這個就是一個符合SOAP規範的檔案了.