JAVA程序访问C#的WebSevice

1、用VS2005建立WebSevice

   在VS中,新建“网站”,选择“ASP.net Web服务”

   修改Service.cs代码:
   using System;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;

using System.Web.Services.Description;



[WebService(Namespace = "http://tempuri.org/")]
//[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[SoapDocumentService(RoutingStyle = SoapServiceRoutingStyle.RequestElement)]
[WebServiceBinding(ConformsTo = WsiProfiles.None)] 
//文章出处:http://www.diybl.com/course/4_webprogram/asp.net/netjs/200863/121183.html

public class Service : System.Web.Services.WebService
{
    public Service () {

        //如果使用设计的组件,请取消注释以下行 
        //InitializeComponent(); 
    }

    [WebMethod]
    public string HelloWorld() {
        return "Hello World 大家好";
    }

    [WebMethod]
    [SoapRpcMethod(Action = "show", RequestNamespace = "http://tempuri.org/")]
    public  string  show(string  type, string name, int ID) 
    {
        return "/n 收到数据:  类别为:" + type + "/n 名称为: " + name + "/n ID为: " + ID.ToString(); 
    }

    [WebMethod]
    public int ADD(int a, int b)
    {
        return a + b;
    }

    
}

这样,WebSevice已经建好,可以直接按F5运行,也可以“生成”-“发布网站”,将功WebSevice发布到IIS中使用。

2、JAVA程序,使用axis访问


public static void show() {
        try {
            String endpoint = "http://localhost:8888/Service.asmx";
            Service service = new Service();
            Call call = (Call) service.createCall();
            call.setTargetEndpointAddress(new java.net.URL(endpoint));
            call.setOperationName(new QName("http://tempuri.org/", "show"));

            call.setUseSOAPAction(true);
            call.setSOAPActionURI("http://tempuri.org/show");
            
//          该方法需要的参数
            call.addParameter("type", org.apache.axis.encoding.XMLType.XSD_STRING,
                    javax.xml.rpc.ParameterMode.IN);
            call.addParameter("name", org.apache.axis.encoding.XMLType.XSD_STRING,
                    javax.xml.rpc.ParameterMode.IN);
            
            call.addParameter("ID", org.apache.axis.encoding.XMLType.XSD_INT,javax.xml.rpc.ParameterMode.IN);
            
            
//          方法的返回值类型
            call.setReturnType(org.apache.axis.encoding.XMLType.XSD_STRING);
            
            
            String s = "水库";
            String name = "大小";
            int ID = 29;
            String res = (String)call.invoke(
                    new Object[]{
                        s,name,ID
                    }
            );
            
            
            System.out.println( "Result: " + res);
        } catch (Exception e) {
            System.err.println(e.toString());
        }
    }

public static void addTest() {
        try {
            Integer i = 1;
            Integer j = 2;
            
            //WebService URL
            String service_url = "http://localhost:1902/donet_web/Service.asmx";
            
            Service service = new Service();
            Call call = (Call) service.createCall();
            call.setTargetEndpointAddress(new java.net.URL(service_url));
            
            //设置要调用的方法
            call.setOperationName(new QName("http://tempuri.org/", "ADD"));
            
            //该方法需要的参数
            call.addParameter("a", org.apache.axis.encoding.XMLType.XSD_INT,
                    javax.xml.rpc.ParameterMode.IN);
            call.addParameter("b", org.apache.axis.encoding.XMLType.XSD_INT,
                    javax.xml.rpc.ParameterMode.IN);
            
            //方法的返回值类型
            call.setReturnType(org.apache.axis.encoding.XMLType.XSD_INT);
            
            call.setUseSOAPAction(true);
            call.setSOAPActionURI("http://tempuri.org/ADD");
            
            //调用该方法
            Integer res = (Integer)call.invoke(
                    new Object[]{
                        i, j
                    }
            );
            
            System.out.println( "Result: " + res.toString());
            
        } catch (Exception e) {
            System.err.println(e);
        }
    }

运行,测试通过,可以很方便的用JAVA代码调用C#编写的WebSevice

几个注意事项:
WebSevice中,调用带参数的方法必须要加:[WebMethod]
    [SoapRpcMethod(Action = "show", RequestNamespace = "http://tempuri.org/")]
不然C#的程序可以正常调用,JAVA的程序不能正常调用
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值