加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

axis,axis2调用.net的webservice

发布时间:2020-12-16 22:05:16 所属栏目:安全 来源:网络整理
导读:今天一个朋友咨询java调用.net的webservice功能,折腾了2个小时,也都折腾出来了,贴出来,希望用到的朋友少走弯路? 1、axis调用.net的webservice? Java代码?? package ?test;?? ?? import ?java.net.URL;?? import ?javax.xml.namespace.QName;?? import ?o
今天一个朋友咨询java调用.net的webservice功能,折腾了2个小时,也都折腾出来了,贴出来,希望用到的朋友少走弯路?
1、axis调用.net的webservice?
Java代码??

收藏代码

  1. package?test;??
  2. ??
  3. import?java.net.URL;??
  4. import?javax.xml.namespace.QName;??
  5. import?org.apache.axis.client.Call;??
  6. import?org.apache.axis.client.Service;??
  7. import?org.apache.axis.encoding.XMLType;??
  8. import?javax.xml.rpc.ParameterMode;??
  9. public?class?Test?{??
  10. ????static?void?test()?throws?Exception{??
  11. ????????Service?service?=?new?Service();??
  12. ????????Call?call?=?null;??
  13. ??????????try?{??
  14. ??????????????call?=?(Call)?service.createCall();??
  15. ??????????????call.setTargetEndpointAddress(new?URL("http://www.webxml.com.cn/WebServices/WeatherWebService.asmx"));??
  16. ??????????????call.setOperationName(new?QName("http://WebXml.com.cn/","getWeatherbyCityName"));??
  17. ??????????????call.addParameter("theCityName"),XMLType.SOAP_VECTOR,ParameterMode.IN);??
  18. ??????????????call.setReturnType(XMLType.SOAP_VECTOR);??
  19. ??????????????call.setUseSOAPAction(true);??
  20. ??????????????call.setSOAPActionURI("http://WebXml.com.cn/getWeatherbyCityName");??
  21. ??????????????System.out.println(call.invoke(new?Object[]{"广州"}));??
  22. ??????????}?catch?(Exception?e)?{??
  23. ??????????????e.printStackTrace();??
  24. ??????????}??
  25. ????}??
  26. ??????
  27. ????/**?
  28. ?????*?@param?args?
  29. ?????*/??
  30. void?main(String[]?args)? ????????test();??
  31. }??


2、axis2调用.net的webservice?
?? axis2调用不需要写那么多,按照下面的步骤,一步一步来,简单你都想象不到?
?? 1、下载axis2(到apache官网下载www.apache.org)?
?? 2、我下载的是axis2-1.5-bin.zip,解压到当前文件夹?
?? 3、进入bin目录(F:studyjavaserviceaxis2axis2-1.5bin)?
?? 4、打开cmd,进入第3步的bin目录,输入wsdl2java.bat -uri http://www.webxml.c?
om.cn/WebServices/WeatherWebService.asmx?wsdl,回车?
?? 5、之后会在bin目录下生成一个src目录,将src目录下的两个类考到eclipse开发目录下?
?? 6、建一个测试类Test,代码如下?
import?cn.com.webxml.WeatherWebServiceStub;??
  • import?cn.com.webxml.WeatherWebServiceStub.ArrayOfString;??
  • import?cn.com.webxml.WeatherWebServiceStub.GetWeatherbyCityName;??
  • void?test1(){??
  • ????????try{??
  • ????????????WeatherWebServiceStub?stub?=?new?WeatherWebServiceStub();??
  • ????????????stub._getServiceClient().getOptions().setProperty(????
  • ????????????????????org.apache.axis2.transport.http.HTTPConstants.CHUNKED,????
  • ????????????????????Boolean.FALSE);??
  • ????????????GetWeatherbyCityName?city?=?new?GetWeatherbyCityName();??
  • ????????????city.setTheCityName("广州");??
  • ????????????ArrayOfString?array?=?stub.getWeatherbyCityName(city).getGetWeatherbyCityNameResult();??
  • ????????????String[]?str?=?array.getString();??
  • ????????????for(String?s?:?str){??
  • ????????????????System.out.println(s);??
  • ????????????}??
  • ????????}catch(Exception?e){??
  • ????????????e.printStackTrace();??
  • ????????}??
  • ????????test1();??
  • 需要注意的是这个类 GetWeatherbyCityName ,这个本来是.net webservice中的一个方法,如下?
    POST?/WebServices/WeatherWebService.asmx?HTTP/1.1??
  • Host:?www.webxml.com.cn??
  • Content-Type:?text/xml;?charset=utf-8??
  • Content-Length:?length??
  • SOAPAction:?"http://WebXml.com.cn/getWeatherbyCityName"??
  • <?xml?version="1.0"?encoding="utf-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/">??
  • ??<soap:Body>??
  • ????<getWeatherbyCityName?xmlns="http://WebXml.com.cn/">??
  • ??????<theCityName>string</theCityName>??
  • ????</getWeatherbyCityName>??
  • ??</soap:Body>??
  • </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;这样才能确保返回正确。?

    比较两个版本,还是觉得axis2使用方便?


    转自http://cqyqing.iteye.com/blog/1668227

    (编辑:李大同)

    【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

      推荐文章
        热点阅读