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

Webservice调用方式:axis,soap详解

发布时间:2020-12-17 01:06:53 所属栏目:安全 来源:网络整理
导读:转自:[url] http://blog.csdn.net/baiboy4493/archive/2009/03/13/3987526.aspx [/url]? 调用webservice,可以首先根据wsdl文件生成客户端,或者直接根据地址调用,下面讨论直接调用地址的两种不同方式:axis和Soap,soap方式主要是用在websphere下? axis方
转自:[url] http://blog.csdn.net/baiboy4493/archive/2009/03/13/3987526.aspx [/url]?
调用webservice,可以首先根据wsdl文件生成客户端,或者直接根据地址调用,下面讨论直接调用地址的两种不同方式:axis和Soap,soap方式主要是用在websphere下?


axis方式调用(Java代码):?

 
 
  1. import?java.util.Date;??
  2. ??
  3. import?java.text.DateFormat;??
  4. import?org.apache.axis.client.Call;??
  5. import?org.apache.axis.client.Service;??
  6. import?javax.xml.namespace.QName;??
  7. import?java.lang.Integer;??
  8. import?javax.xml.rpc.ParameterMode;??
  9. public?class?caClient?{??
  10. static?void?main(String[]?args)?{??
  11. try?{??
  12. String?endpoint?=?"http://localhost:8080/ca3/services/caSynrochnized?wsdl";??
  13. Service?service?=?new?Service();??
  14. Call?call?=?(Call)?service.createCall();??
  15. call.setTargetEndpointAddress(endpoint);??
  16. call.setOperationName("addUser");??
  17. call.addParameter("userName",?org.apache.axis.encoding.XMLType.XSD_DATE,??
  18. javax.xml.rpc.ParameterMode.IN);??
  19. call.setReturnType(org.apache.axis.encoding.XMLType.XSD_STRING);??
  20. call.setUseSOAPAction(true);??
  21. call.setSOAPActionURI("http://www.my.com/Rpc");??
  22. //Integer?k?=?(Integer)?call.invoke(new?Object[]?{?i,?j?});??
  23. //System.out.println("result?is?"?+?k.toString()?+?".");??
  24. String?temp?=?"测试人员";??
  25. String?result?=?(String)call.invoke(new?Object[]{temp});??
  26. System.out.println("result?is?"+result);??
  27. }??
  28. catch?(Exception?e)?{??
  29. System.err.println(e.toString());??
  30. }??

soap方式调用?

调用java生成的webservice (Java代码 )

  1. import?org.apache.soap.util.xml.*;??
  2. ??
  3. import?org.apache.soap.*;??
  4. import?org.apache.soap.rpc.*;??
  5. import?java.io.*;??
  6. import?java.net.*;??
  7. import?java.util.Vector;??
  8. public?class?caService{??
  9. static?String?getService(String?user)?{??
  10. URL?url?=?null;??
  11. try?{??
  12. url=new?URL("http://192.168.0.100:8080/ca3/services/caSynrochnized");??
  13. }?catch?(MalformedURLException?mue)?{??
  14. return?mue.getMessage();??
  15. }??
  16. //?This?is?the?main?SOAP?object??
  17. Call?soapCall?=?new?Call();??
  18. //?Use?SOAP?encoding??
  19. soapCall.setEncodingStyleURI(Constants.NS_URI_SOAP_ENC);??
  20. //?This?is?the?remote?object?we're?asking?for?the?price??
  21. soapCall.setTargetObjectURI("urn:xmethods-caSynrochnized");??
  22. //?This?is?the?name?of?the?method?on?the?above?object??
  23. soapCall.setMethodName("getUser");??
  24. //?We?need?to?send?the?ISBN?number?as?an?input?parameter?to?the?method??
  25. Vector?soapParams?=?new?Vector();??
  26. //?name,?type,?value,?encoding?style??
  27. Parameter?isbnParam?=?new?Parameter("userName",?String.class,?user,?null);??
  28. soapParams.addElement(isbnParam);??
  29. soapCall.setParams(soapParams);??
  30. //?Invoke?the?remote?method?on?the?object??
  31. Response?soapResponse?=?soapCall.invoke(url,"");??
  32. //?Check?to?see?if?there?is?an?error,?return?"N/A"??
  33. if?(soapResponse.generatedFault())?{??
  34. Fault?fault?=?soapResponse.getFault();??
  35. String?f?=?fault.getFaultString();??
  36. return?f;??
  37. else?{??
  38. //?read?result??
  39. Parameter?soapResult?=?soapResponse.getReturnValue?();??
  40. //?get?a?string?from?the?result??
  41. return?soapResult.getValue().toString();??
  42. catch?(SOAPException?se)?{??
  43. return?se.getMessage();??
  44. }??


返回一维数组时 (Java代码)
Parameter?soapResult?=?soapResponse.getReturnValue();??
  • String[]?temp?=?(String[])soapResult.getValue();??


  • 调用ASP.Net生成的webservice (Java代码)
    private?String?HelloWorld(String?uri,?String?u)?{??
  • SOAPMappingRegistry?smr?=?new?SOAPMappingRegistry();??
  • StringDeserializer?sd?=?new?StringDeserializer();??
  • ArraySerializer?arraySer?=?new?ArraySerializer();??
  • BeanSerializer?beanSer?=?new?BeanSerializer();??
  • smr.mapTypes(Constants.NS_URI_SOAP_ENC,?new?QName(??
  • "http://tempuri.org/",?"HelloWorldResult"),??
  • null,?sd);??
  • "temp"),250); line-height:18px"> beanSer,?beanSer);??
  • new?URL(uri);??
  • Call?call?=? call.setSOAPMappingRegistry(smr);??
  • call.setTargetObjectURI("urn:xmethods-Service1");??
  • call.setMethodName("HelloWorld");??
  • call.setEncodingStyleURI(Constants.NS_URI_SOAP_ENC);??
  • soapParams.addElement(new?Parameter("temp",?u,85); font-weight:bold">null));??
  • call.setParams(soapParams);??
  • Response?soapResponse?=?call.invoke(url,"http://tempuri.org/HelloWorld");??
  • System.out.println(fault);??
  • Parameter?soapResult?=?soapResponse.getReturnValue();??
  • Object?obj?=?soapResult.getValue();??
  • System.out.println("==="?+?obj);??
  • catch?(Exception?e)?{??
  • e.printStackTrace();??
  • return? }???
  • /**?
  • ??*?调用?C#?的webservice接口?
  • ??*?SoapRpcMethod(Action?=?"http://www.tangs.com/Add",??
  • ??*?RequestNamespace?=?"http://www.tangs.com/T",0)">??*?ResponseNamespace?=?"http://www.tangs.com/T",0)">??*?Use?=?SoapBindingUse.Literal)]?
  • ??*??
  • ??*?*/??
  • ?static?void?addTest()?{??
  • ?? ???Integer?i?=?1;??
  • ???Integer?j?=?2;??
  • ???//?WebService?URL??
  • ???String?service_url?=?"http://localhost:4079/ws/Service.asmx";??
  • ???Service?service?=?new?Service();??
  • ???Call?call?=?(Call)?service.createCall();??
  • ???call.setTargetEndpointAddress(new?java.net.URL(service_url));??
  • //?设置要调用的方法??
  • ???call.setOperationName(new?QName("http://www.tangs.com/T",?"Add"));??
  • //?该方法需要的参数??
  • ???call.addParameter("a",?org.apache.axis.encoding.XMLType.XSD_INT,?javax.xml.rpc.ParameterMode.IN);??
  • ???call.addParameter("b",0)">//?方法的返回值类型??
  • ???call.setReturnType(org.apache.axis.encoding.XMLType.XSD_INT);??
  • ???call.setUseSOAPAction(true);??
  • ???call.setSOAPActionURI("http://www.tangs.com/Add");??
  • //?调用该方法??
  • ???Integer?res?=?(Integer)?call.invoke(new?Object[]?{?i,?j?});??
  • ???System.out.println("Result:?"?+?res.toString());??
  • ??}? ???System.err.println(e);??
  • ??}??
  • ?}??
  • ?void?helloTest()?{??
  • ???String?endpoint?=?"http://localhost:4079/ws/Service.asmx";??
  • new?java.net.URL(endpoint));??
  • "HelloWorld"));??
  • ???call.setSOAPActionURI("http://www.tangs.com/Hello");??
  • ???String?res?=?(String)?call.invoke(new?Object[]?{?null?});??
  • ???System.out.println("Result:?"?+?res);??
  • ???System.err.println(e.toString());??
  • ?} ?
  • (编辑:李大同)

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

      推荐文章
        热点阅读