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


Rpc风格的.net服务端代码:
Java代码 复制代码
  1. using System;   
  2. using System.Collections;   
  3. using System.ComponentModel;   
  4. using System.Data;   
  5. using System.Linq;   
  6. using System.Web;   
  7. using System.Web.Services;   
  8. using System.Web.Services.Protocols;   
  9. using System.Xml.Linq;   
  10.   
  11. namespace WebService1   
  12. {       
  13.        
  14.     [WebService(Namespace = "http://tempuri.org/")]   
  15.     public class Service1 : System.Web.Services.WebService   
  16.     {   
  17.   
  18.         [WebMethod]   
  19.         [SoapRpcMethod(Action = "http://tempuri.org/hello", RequestNamespace = "http://tempuri.org/", ResponseNamespace = "http://tempuri.org/")]   
  20.   
  21.         public string hello(String s)   
  22.         {   
  23.             return "Hello,"+s;   
  24.         }   
  25.     }   
  26. }  


Document风格的.net服务端代码:
Java代码 复制代码
  1. namespace WebService1   
  2. {       
  3.        
  4.     [WebService(Namespace = "http://tempuri.org/")]   
  5.     public class Service1 : System.Web.Services.WebService   
  6.     {   
  7.   
  8.         [WebMethod]           
  9.         public string hello(String s)   
  10.         {   
  11.             return "Hello,"+s;   
  12.         }   
  13.     }   
  14. }  


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



如果.net在提供Rpc服务时设置成
Java代码 复制代码
  1. [SoapRpcMethod(Action = "",.....  

的话,那java客户端连
Java代码 复制代码
  1. call.setUseSOAPAction(true);   
  2. call.setSOAPActionURI(soapActionURI);  

这2句都可以去掉了
posted on 2008-12-26 15:19  许维光  阅读(1098)  评论(0)    收藏  举报