C#.net調用axis2webService

來源:互聯網
上載者:User
//java serviceimport java.util.Date;public class SimpleService {public String getGreeting(String name) {return "你好 " + name+",現在時間是:"+new Date();}public int getPrice() {return new java.util.Random().nextInt(1000);}}
放入:%TOMCAT_HOME%\webapps\axis2\WEB-INF\pojo下,若沒有pojo,則建立一個pojo目錄.java client 法1:
package test;import javax.xml.namespace.QName;import org.apache.axis2.addressing.EndpointReference;import org.apache.axis2.client.Options;import org.apache.axis2.rpc.client.RPCServiceClient;public class TestSimpleService {/** * @param args */public static void main(String[] args) throws Throwable{ //  使用RPC方式調用WebService                RPCServiceClient serviceClient = new RPCServiceClient();        Options options = serviceClient.getOptions();        //  指定調用WebService的URL        EndpointReference targetEPR = new EndpointReference(                "http://localhost:7777/axis2/services/SimpleService");        options.setTo(targetEPR);        //  指定getGreeting方法的參數值        Object[] opAddEntryArgs = new Object[] {"超人"};        //  指定getGreeting方法傳回值的資料類型的Class對象        Class[] classes = new Class[] {String.class};        //  指定要調用的getGreeting方法及WSDL檔案的命名空間        QName opAddEntry = new QName("http://ws.apache.org/axis2", "getGreeting");        //  調用getGreeting方法並輸出該方法的傳回值        System.out.println(serviceClient.invokeBlocking(opAddEntry, opAddEntryArgs, classes)[0]);        //  下面是調用getPrice方法的代碼,這些代碼與調用getGreeting方法的代碼類似        classes = new Class[] {int.class};        opAddEntry = new QName("http://ws.apache.org/axis2", "getPrice");        System.out.println(serviceClient.invokeBlocking(opAddEntry, new Object[]{}, classes)[0]);}}
java法2:

  在Windows控制台輸出如下的命令列來產生調用WebService的代碼:

%AXIS2_HOME%\bin\wsdl2java -uri http://localhost:8080/axis2/services/SimpleService?wsdl -p client -s -o stub

    其中-url參數指定了wsdl檔案的路徑,可以是本地路徑,也可以是網路路徑。-p參數指定了產生的Java類的包名,-o參數指定了產生的一系列檔案儲存的根目錄。在執行完上面的命令後,讀者就會發現在目前的目錄下多了個stub目錄,在."stub"src"client目錄可以找到一個SimpleServiceStub.java檔案,該檔案複雜調用WebService,讀者可以在程式中直接使用這個類,代碼如下:

package test;import client.SimpleServiceStub;public class TestSimpleServiceWithClientStub {/** * @param args */public static void main(String[] args) throws Throwable {SimpleServiceStub stub = new SimpleServiceStub();SimpleServiceStub.GetGreeting gg = new SimpleServiceStub.GetGreeting();gg.setName("一鬨而散在");String sGreetingResult = stub.getGreeting(gg).get_return();System.out.println(sGreetingResult);int iPriceResult = stub.getPrice().get_return();System.out.println(iPriceResult);}}
.net client 法3:
using System;using System.Collections.Generic;using System.Linq;using System.Text;namespace TestAxis2ConsoleApp{    class Program    {        static void Main(string[] args)        {            //ServiceReference1.SimpleServicePortTypeClient client = new TestAxis2ConsoleApp.ServiceReference1.SimpleServicePortTypeClient("SimpleServiceHttpSoap11Endpoint");            ServiceReference1.SimpleServicePortTypeClient client = new TestAxis2ConsoleApp.ServiceReference1.SimpleServicePortTypeClient("SimpleServiceHttpSoap12Endpoint");                                string s= client.getGreeting("吳xx");            Console.WriteLine(s);            Console.WriteLine();            int price =client.getPrice();            Console.WriteLine(price);            Console.ReadKey();        }    }}
相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.