`
kusix
  • 浏览: 67766 次
社区版块
存档分类
最新评论

axis调用两种风格的.net WebService

阅读更多
最近做一个接口需要调用.net提供的WebService,调试过程很痛苦,总是出现Server was unable to read request的错误,并且.net服务端怎么都接不到传送的参数,接到的都是null。在网上查了半天资料也没有解决,最后下了个vs2008自己来写.net的服务学习了一下,才把问题搞定。


Rpc风格的.net服务端代码:
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Xml.Linq;

namespace WebService1
{    
    
    [WebService(Namespace = "http://tempuri.org/")]
    public class Service1 : System.Web.Services.WebService
    {

        [WebMethod]
        [SoapRpcMethod(Action = "http://tempuri.org/hello", RequestNamespace = "http://tempuri.org/", ResponseNamespace = "http://tempuri.org/")]

        public string hello(String s)
        {
            return "Hello,"+s;
        }
    }
}


Document风格的.net服务端代码:
namespace WebService1
{    
    
    [WebService(Namespace = "http://tempuri.org/")]
    public class Service1 : System.Web.Services.WebService
    {

        [WebMethod]        
        public string hello(String s)
        {
            return "Hello,"+s;
        }
    }
}


java客户端代码:
String url="http://localhost:1117/Service1.asmx";
String namespace = "http://tempuri.org/";
String methodName = "hello";
String soapActionURI = "http://tempuri.org/hello";
Service service = new Service();
Call call = (Call) service.createCall();
call.setTargetEndpointAddress(new java.net.URL(url));
call.setUseSOAPAction(true);
//这个地方没设对就会出现Server was unable to read request的错误
call.setSOAPActionURI(soapActionURI);
call.setOperationName(new QName(namespace, methodName));
/*这里如果设置成call.addParameter(new QName(namespace,"s"), XMLType.XSD_STRING, 
  ParameterMode.IN);就是调用document风格的.net服务端
  如果设反了,.net服务端就接不到参数,接到的是null
*/
call.addParameter("s", XMLType.XSD_STRING, ParameterMode.IN);
call.setReturnType(XMLType.XSD_STRING);
String ret = (String) call.invoke(new Object[] { "kusix" });
System.out.println("返回结果---> " + ret);



如果.net在提供Rpc服务时设置成
[SoapRpcMethod(Action = "",.....

的话,那java客户端连
call.setUseSOAPAction(true);
call.setSOAPActionURI(soapActionURI);

这2句都可以去掉了

分享到:
评论
2 楼 ritcher 2008-12-04  
讲得很清晰,解决了问题,引用了:)
1 楼 mazhi21tt 2008-08-06  
好!捧场~~~~

相关推荐

Global site tag (gtag.js) - Google Analytics