zoukankan      html  css  js  c++  java
  • Webservice调用方式:axis,soap详解

    转自:[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.   
    5. import org.apache.axis.client.Call;  
    6.   
    7. import org.apache.axis.client.Service;  
    8.   
    9. import javax.xml.namespace.QName;  
    10.   
    11. import java.lang.Integer;  
    12.   
    13. import javax.xml.rpc.ParameterMode;  
    14.   
    15.   
    16. public class caClient {  
    17.   
    18.   
    19.   
    20. public static void main(String[] args) {  
    21.   
    22.   
    23. try {  
    24.   
    25. String endpoint = "http://localhost:8080/ca3/services/caSynrochnized?wsdl";  
    26.   
    27. Service service = new Service();  
    28.   
    29. Call call = (Call) service.createCall();  
    30.   
    31. call.setTargetEndpointAddress(endpoint);  
    32.   
    33. call.setOperationName("addUser");  
    34.   
    35. call.addParameter("userName", org.apache.axis.encoding.XMLType.XSD_DATE,  
    36.   
    37. javax.xml.rpc.ParameterMode.IN);  
    38.   
    39. call.setReturnType(org.apache.axis.encoding.XMLType.XSD_STRING);  
    40.   
    41. call.setUseSOAPAction(true);  
    42.   
    43. call.setSOAPActionURI("http://www.my.com/Rpc");  
    44.   
    45. //Integer k = (Integer) call.invoke(new Object[] { i, j });  
    46.   
    47. //System.out.println("result is " + k.toString() + ".");  
    48.   
    49. String temp = "测试人员";  
    50.   
    51. String result = (String)call.invoke(new Object[]{temp});  
    52.   
    53. System.out.println("result is "+result);  
    54.   
    55. }  
    56.   
    57. catch (Exception e) {  
    58.   
    59. System.err.println(e.toString());  
    60.   
    61. }  
    62.   
    63. }  
    64.   
    65. }  

    soap方式调用 

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

    1. import org.apache.soap.util.xml.*;  
    2.   
    3. import org.apache.soap.*;  
    4.   
    5. import org.apache.soap.rpc.*;  
    6.   
    7.   
    8. import java.io.*;  
    9.   
    10. import java.net.*;  
    11.   
    12. import java.util.Vector;  
    13.   
    14.   
    15. public class caService{  
    16.   
    17. public static String getService(String user) {  
    18.   
    19. URL url = null;  
    20.   
    21. try {  
    22.   
    23. url=new URL("http://192.168.0.100:8080/ca3/services/caSynrochnized");  
    24.   
    25. catch (MalformedURLException mue) {  
    26.   
    27. return mue.getMessage();  
    28.   
    29. }  
    30.   
    31. // This is the main SOAP object  
    32.   
    33. Call soapCall = new Call();  
    34.   
    35. // Use SOAP encoding  
    36.   
    37. soapCall.setEncodingStyleURI(Constants.NS_URI_SOAP_ENC);  
    38.   
    39. // This is the remote object we're asking for the price  
    40.   
    41. soapCall.setTargetObjectURI("urn:xmethods-caSynrochnized");  
    42.   
    43. // This is the name of the method on the above object  
    44.   
    45. soapCall.setMethodName("getUser");  
    46.   
    47. // We need to send the ISBN number as an input parameter to the method  
    48.   
    49. Vector soapParams = new Vector();  
    50.   
    51.   
    52. // name, type, value, encoding style  
    53.   
    54. Parameter isbnParam = new Parameter("userName", String.class, user, null);  
    55.   
    56. soapParams.addElement(isbnParam);  
    57.   
    58. soapCall.setParams(soapParams);  
    59.   
    60. try {  
    61.   
    62. // Invoke the remote method on the object  
    63.   
    64. Response soapResponse = soapCall.invoke(url,"");  
    65.   
    66. // Check to see if there is an error, return "N/A"  
    67.   
    68. if (soapResponse.generatedFault()) {  
    69.   
    70. Fault fault = soapResponse.getFault();  
    71.   
    72. String f = fault.getFaultString();  
    73.   
    74. return f;  
    75.   
    76. else {  
    77.   
    78. // read result  
    79.   
    80. Parameter soapResult = soapResponse.getReturnValue ();  
    81.   
    82. // get a string from the result  
    83.   
    84. return soapResult.getValue().toString();  
    85.   
    86. }  
    87.   
    88. catch (SOAPException se) {  
    89.   
    90. return se.getMessage();  
    91.   
    92. }  
    93.   
    94. }  
    95.   
    96. }  


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


    调用ASP.Net生成的webservice (Java代码)
    1. private String HelloWorld(String uri, String u) {  
    2.   
    3. try {  
    4.   
    5. SOAPMappingRegistry smr = new SOAPMappingRegistry();  
    6.   
    7. StringDeserializer sd = new StringDeserializer();  
    8.   
    9. ArraySerializer arraySer = new ArraySerializer();  
    10.   
    11. BeanSerializer beanSer = new BeanSerializer();  
    12.   
    13. smr.mapTypes(Constants.NS_URI_SOAP_ENC, new QName(  
    14.   
    15. "http://tempuri.org/""HelloWorldResult"), String.class,  
    16.   
    17. null, sd);  
    18.   
    19. smr.mapTypes(Constants.NS_URI_SOAP_ENC, new QName(  
    20.   
    21. "http://tempuri.org/""temp"), String.class,  
    22.   
    23. beanSer, beanSer);  
    24.   
    25.   
    26. URL url = new URL(uri);  
    27.   
    28. Call call = new Call();  
    29.   
    30. call.setSOAPMappingRegistry(smr);  
    31.   
    32. call.setTargetObjectURI("urn:xmethods-Service1");  
    33.   
    34. call.setMethodName("HelloWorld");  
    35.   
    36. call.setEncodingStyleURI(Constants.NS_URI_SOAP_ENC);  
    37.   
    38.   
    39. Vector soapParams = new Vector();  
    40.   
    41. soapParams.addElement(new Parameter("temp", String.class, u, null));  
    42.   
    43. call.setParams(soapParams);  
    44.   
    45.   
    46. Response soapResponse = call.invoke(url,"http://tempuri.org/HelloWorld");  
    47.   
    48.   
    49. if (soapResponse.generatedFault()) {  
    50.   
    51. Fault fault = soapResponse.getFault();  
    52.   
    53. System.out.println(fault);  
    54.   
    55. else {  
    56.   
    57. Parameter soapResult = soapResponse.getReturnValue();  
    58.   
    59. Object obj = soapResult.getValue();  
    60.   
    61. System.out.println("===" + obj);  
    62.   
    63. }  
    64.   
    65. catch (Exception e) {  
    66.   
    67. e.printStackTrace();  
    68.   
    69. }  
    70.   
    71. return null;  
    72. }   
    73. /** 
    74.   * 调用 C# 的webservice接口 
    75.   * SoapRpcMethod(Action = "http://www.tangs.com/Add",  
    76.   * RequestNamespace = "http://www.tangs.com/T",  
    77.   * ResponseNamespace = "http://www.tangs.com/T",  
    78.   * Use = SoapBindingUse.Literal)] 
    79.   *  
    80.   * */  
    81.  public static void addTest() {  
    82.   try {  
    83.    Integer i = 1;  
    84.    Integer j = 2;  
    85.   
    86.    // WebService URL  
    87.    String service_url = "http://localhost:4079/ws/Service.asmx";  
    88.   
    89.    Service service = new Service();  
    90.    Call call = (Call) service.createCall();  
    91.    call.setTargetEndpointAddress(new java.net.URL(service_url));  
    92.   
    93.    // 设置要调用的方法  
    94.    call.setOperationName(new QName("http://www.tangs.com/T""Add"));  
    95.   
    96.    // 该方法需要的参数  
    97.    call.addParameter("a", org.apache.axis.encoding.XMLType.XSD_INT, javax.xml.rpc.ParameterMode.IN);  
    98.    call.addParameter("b", org.apache.axis.encoding.XMLType.XSD_INT, javax.xml.rpc.ParameterMode.IN);  
    99.   
    100.    // 方法的返回值类型  
    101.    call.setReturnType(org.apache.axis.encoding.XMLType.XSD_INT);  
    102.   
    103.    call.setUseSOAPAction(true);  
    104.    call.setSOAPActionURI("http://www.tangs.com/Add");  
    105.   
    106.    // 调用该方法  
    107.    Integer res = (Integer) call.invoke(new Object[] { i, j });  
    108.   
    109.    System.out.println("Result: " + res.toString());  
    110.   
    111.   } catch (Exception e) {  
    112.    System.err.println(e);  
    113.   }  
    114.  }  
    115.   
    116.  /** 
    117.   * 调用 C# 的webservice接口 
    118.   * SoapRpcMethod(Action = "http://www.tangs.com/Add",  
    119.   * RequestNamespace = "http://www.tangs.com/T",  
    120.   * ResponseNamespace = "http://www.tangs.com/T",  
    121.   * Use = SoapBindingUse.Literal)] 
    122.   *  
    123.   * */  
    124.  public static void helloTest() {  
    125.   try {  
    126.   
    127.    String endpoint = "http://localhost:4079/ws/Service.asmx";  
    128.    Service service = new Service();  
    129.    Call call = (Call) service.createCall();  
    130.    call.setTargetEndpointAddress(new java.net.URL(endpoint));  
    131.    call.setOperationName(new QName("http://www.tangs.com/T""HelloWorld"));  
    132.   
    133.    call.setUseSOAPAction(true);  
    134.    call.setSOAPActionURI("http://www.tangs.com/Hello");  
    135.   
    136.    String res = (String) call.invoke(new Object[] { null });  
    137.   
    138.    System.out.println("Result: " + res);  
    139.   } catch (Exception e) {  
    140.    System.err.println(e.toString());  
    141.   }  
    142.  }  

  • 相关阅读:
    背景100%显示
    你还在用Html.PartialAsync? 教你更简单一招搞定
    记一次TokuMX数据库集群恢复
    使用SolrNet访问Solr-5.5.0
    从注册表中删除打印机列表中的打印机
    DEV之GridControl常用设置
    在Devexpress中设置按钮列(无需写代码)
    Sql中获取表结构(字段名称,类型,长度,说明)
    DevExpress之ChartControl基本用法
    SQL Server类型与C#类型对应关系[转]
  • 原文地址:https://www.cnblogs.com/y0umer/p/3838993.html
Copyright © 2011-2022 走看看