中学音乐老师:java通过axis访问.net webService 简单例子 - - JavaEye技术网站

来源:百度文库 编辑:偶看新闻 时间:2024/05/04 07:18:50
关键字: java axis原文地址:http://www.javaeye.com/topic/151541
在原文基础上加了点注释 呵呵。

所需jar包:
saaj.jar
wsdl4j-1.5.1.jar
commons-discovery-0.2.jar
commons-logging-1.0.4.jar
jaxrpc.jar
axis.jar

这些jar包都在axis项目下,
axis下载地址:http://ws.apache.org/axis/

java代码:
Java代码  
  1. // 需导入的类  
  2. import javax.xml.namespace.QName;  
  3. import org.apache.axis.client.Call;  
  4. import org.apache.axis.client.Service;  
  5. import org.apache.axis.encoding.XMLType;  
  6.   
  7. //              .net webService 地址  
  8.         String url="http://localhost:1246/WebSite3/Service.asmx";    
  9. //              .net webService 命名空间  
  10.         String namespace = "http://tempuri.org/";    
  11. //              .net webService 需调用的方法  
  12.         String methodName = "HelloWorld";    
  13.         String soapActionURI = "http://tempuri.org/HelloWorld";    
  14.         Service service = new Service();  
  15.           
  16.         Call call = (Call) service.createCall();  
  17.   
  18.         call.setTargetEndpointAddress(new java.net.URL(url));    
  19.         call.setUseSOAPAction(true);    
  20. //      这个地方没设对就会出现Server was unable to read request的错误    
  21.         call.setSOAPActionURI(soapActionURI);    
  22. //              设置要调用的.net webService方法  
  23.         call.setOperationName(new QName(namespace, methodName));    
  24. //              设置该方法的参数,temp为.net webService中的参数名称  
  25.         call.addParameter( new QName(namespace,"temp"),    
  26.                 org.apache.axis.encoding.XMLType.XSD_STRING,     
  27.                 javax.xml.rpc.ParameterMode.IN);    
  28. //              设置该方法的返回值  
  29.         call.setReturnType(XMLType.XSD_STRING);  
  30. //              call.invoke(new Object[] { "kusix" });  中"kusix"为传入参数值  
  31.         String ret = (String) call.invoke(new Object[] { "kusix" });    
  32.         System.out.println("返回结果---> " + ret);    


.net 代码
C#代码  
  1. using System;  
  2. using System.Web;  
  3. using System.Web.Services;  
  4. using System.Web.Services.Protocols;  
  5.   
  6. [WebService(Namespace = "http://tempuri.org/")]  
  7. [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]  
  8. public class Service : System.Web.Services.WebService  
  9. {  
  10.     public Service () {  
  11.   
  12.         //如果使用设计的组件,请取消注释以下行   
  13.         //InitializeComponent();   
  14.     }  
  15.   
  16.     [WebMethod]  
  17.     public string HelloWorld(String temp) {  
  18.         return temp+" add by .net";  
  19.     }  
  20.       
  21. }