axis调用webservice(net)

今天一个朋友咨询java调用.net的webservice功能,折腾了2个小时,也都折腾出来了,贴出来,希望用到的朋友少走弯路 
1、axis调用.net的webservice 
Java代码   收藏代码
  1. package test;  
  2.   
  3. import java.net.URL;  
  4.   
  5. import javax.xml.namespace.QName;  
  6.   
  7. import org.apache.axis.client.Call;  
  8. import org.apache.axis.client.Service;  
  9. import org.apache.axis.encoding.XMLType;  
  10. import javax.xml.rpc.ParameterMode;  
  11.   
  12. public class Test {  
  13.   
  14.     public static void test() throws Exception{  
  15.         Service service = new Service();  
  16.         Call call = null;  
  17.           try {  
  18.               call = (Call) service.createCall();  
  19.               call.setTargetEndpointAddress(new URL("http://www.webxml.com.cn/WebServices/WeatherWebService.asmx"));  
  20.               call.setOperationName(new QName("http://WebXml.com.cn/","getWeatherbyCityName"));  
  21.               call.addParameter(new QName("http://WebXml.com.cn/""theCityName"),XMLType.SOAP_VECTOR,ParameterMode.IN);  
  22.               call.setReturnType(XMLType.SOAP_VECTOR);  
  23.               call.setUseSOAPAction(true);  
  24.               call.setSOAPActionURI("http://WebXml.com.cn/getWeatherbyCityName");  
  25.               System.out.println(call.invoke(new Object[]{"广州"}));  
  26.           } catch (Exception e) {  
  27.               e.printStackTrace();  
  28.           }  
  29.     }  
  30.       
  31.     /** 
  32.      * @param args 
  33.      */  
  34.     public static void main(String[] args) throws Exception{  
  35.         test();  
  36.     }  
  37.   
  38. }  


2、axis2调用.net的webservice 
   axis2调用不需要写那么多,按照下面的步骤,一步一步来,简单你都想象不到 
   1、下载axis2(到apache官网下载www.apache.org) 
   2、我下载的是axis2-1.5-bin.zip,解压到当前文件夹 
   3、进入bin目录(F:\study\java\service\axis2\axis2-1.5\bin) 
   4、打开cmd,进入第3步的bin目录,输入wsdl2java.bat -uri http://www.webxml.c 
om.cn/WebServices/WeatherWebService.asmx?wsdl,回车 
   5、之后会在bin目录下生成一个src目录,将src目录下的两个类考到eclipse开发目录下 
   6、建一个测试类Test,代码如下 
Java代码   收藏代码
  1. import cn.com.webxml.WeatherWebServiceStub;  
  2. import cn.com.webxml.WeatherWebServiceStub.ArrayOfString;  
  3. import cn.com.webxml.WeatherWebServiceStub.GetWeatherbyCityName;  
  4.   
  5.   
  6. public class Test {  
  7.   
  8.     public static void test1(){  
  9.         try{  
  10.             WeatherWebServiceStub stub = new WeatherWebServiceStub();  
  11.             stub._getServiceClient().getOptions().setProperty(    
  12.                     org.apache.axis2.transport.http.HTTPConstants.CHUNKED,    
  13.                     Boolean.FALSE);  
  14.             GetWeatherbyCityName city = new GetWeatherbyCityName();  
  15.             city.setTheCityName("广州");  
  16.             ArrayOfString array = stub.getWeatherbyCityName(city).getGetWeatherbyCityNameResult();  
  17.             String[] str = array.getString();  
  18.             for(String s : str){  
  19.                 System.out.println(s);  
  20.             }  
  21.         }catch(Exception e){  
  22.             e.printStackTrace();  
  23.         }  
  24.     }  
  25.       
  26.     /** 
  27.      * @param args 
  28.      */  
  29.     public static void main(String[] args) throws Exception{  
  30.         test1();  
  31.     }  
  32.   
  33. }  

需要注意的是这个类 GetWeatherbyCityName ,这个本来是.net webservice中的一个方法,如下 
Java代码   收藏代码
  1. POST /WebServices/WeatherWebService.asmx HTTP/1.1  
  2. Host: www.webxml.com.cn  
  3. Content-Type: text/xml; charset=utf-8  
  4. Content-Length: length  
  5. SOAPAction: "http://WebXml.com.cn/getWeatherbyCityName"  
  6.   
  7. <?xml version="1.0" encoding="utf-8"?>  
  8. <soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">  
  9.   <soap:Body>  
  10.     <getWeatherbyCityName xmlns="http://WebXml.com.cn/">  
  11.       <theCityName>string</theCityName>  
  12.     </getWeatherbyCityName>  
  13.   </soap:Body>  
  14. </soap:Envelope>  

用axis2生成java代码后,会自动生成一个对应的对象,webservice需要传递的参数,可以通过对这个对象赋值操作完成,如上面,我要查广州的天气,就设置为city.setTheCityName("广州"); 
注意,关键的地方  
由于.net webservice中返回的是ArrayOfString,java中没有这个对象,所以axis2会自动生成这个对象,然后转换成对应的数组即可,如String[] str = array.getString();在axis版本中,使用的是返回类型,但是返回类型设置其他的比如String等都会报错,只能设置成VECTOR,即call.setReturnType(XMLType.SOAP_VECTOR),如果只返回一个字符串,可以直接使用STRING;这样才能确保返回正确。 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值