Axis入門(3)

來源:互聯網
上載者:User

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規範的檔案了.

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.