C#.net调用axis2webService

//java service
import 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();
        }
    }
}
posted @ 2011-07-13 16:00  庚武  Views(4625)  Comments(0Edit  收藏  举报