zoukankan      html  css  js  c++  java
  • java调用webservice接口 几种方法

    webservice的 发布一般都是使用WSDL(web service descriptive language)文件的样式来发布的,在WSDL文件里面,包含这个webservice暴露在外面可供使用的接口。今天搜索到了非常好的 webservice provider列表

    http://www.webservicex.net/WCF/default.aspx

    这上面列出了70多个包括很多方面的free webservice provider,utilities->global weather就可以获取全球的天气预报。

    下面我们来看Java如何通过WSDL文件来调用这些web service:

    注意,以下的代码并没有经过真正的测试,只是说明这些情况,不同版本的Axis相差很大,大家最好以apache网站上的例子为准,这里仅仅用于说明其基本用法。

    1,直接AXIS调用远程的web service

    我觉得这种方法比较适合那些高手,他们能直接看懂XML格式的WSDL文件,我自己是看不懂的,尤其我不是专门搞这行的,即使一段时间看懂,后来也就忘记了。直接调用模式如下:

    [java] view plain copy
     
    1. import java.util.Date;  
    2. import java.text.DateFormat;  
    3. import org.apache.axis.client.Call;  
    4. import org.apache.axis.client.Service;  
    5. import javax.xml.namespace.QName;  
    6. import java.lang.Integer;  
    7. import javax.xml.rpc.ParameterMode;  
    8.   
    9. public class caClient {  
    10.     public static void main(String[] args) {  
    11.   
    12.         try {  
    13.             String endpoint = "http://localhost:8080/ca3/services/caSynrochnized?wsdl";  
    14.             // 直接引用远程的wsdl文件  
    15.             // 以下都是套路  
    16.             Service service = new Service();  
    17.             Call call = (Call) service.createCall();  
    18.             call.setTargetEndpointAddress(endpoint);  
    19.             call.setOperationName("addUser");// WSDL里面描述的接口名称  
    20.             call.addParameter("userName",  
    21.                     org.apache.axis.encoding.XMLType.XSD_DATE,  
    22.                     javax.xml.rpc.ParameterMode.IN);// 接口的参数  
    23.             call.setReturnType(org.apache.axis.encoding.XMLType.XSD_STRING);// 设置返回类型  
    24.             String temp = "测试人员";  
    25.             String result = (String) call.invoke(new Object[] { temp });  
    26.             // 给方法传递参数,并且调用方法  
    27.             System.out.println("result is " + result);  
    28.         } catch (Exception e) {  
    29.             System.err.println(e.toString());  
    30.         }  
    31.     }  
    32. }  



    2,直接SOAP调用远程的webservice

    这种模式我从来没有见过,也没有试过,但是网络上有人贴出来,我也转过来

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

    3,使用wsdl2java把WSDL文件转成本地类,然后像本地类一样使用,即可。

    这是像我这种懒人最喜欢的方式,仍然以前面的global weather report为例。

    首先 java org.apache.axis.wsdl.WSDL2Java http://www.webservicex.net/globalweather.asmx.WSDL

    原本的网址是http://www.webservicex.net/globalweather.asmx?WSDL,中间个各问号,但是Linux下面它不能解析,所以去掉问号,改为点号。

    那么就会出现4个文件:

    GlobalWeather.java 

    GlobalWeatherLocator.java 

    GlobalWeatherSoap.java

    GlobalWeatherSoapStub.java
    其中GlobalWeatherSoap.java是我们最为关心的接口文件,如果你对RMI等SOAP实现的具体细节不感兴趣,那么你只需要看接口文件即可,

    在使用的时候,引入这个接口即可,就好像使用本地类一样。

  • 相关阅读:
    套接口编程理论基础:正常启动
    套接口编程理论基础:服务器进程终止
    套接口编程理论基础:处理SIGCHLD信号
    分区表、分区索引
    IPC通信:Posix消息队列读,写
    IPC通信:Posix消息队列的创建,关闭,删除
    RBAC的资料
    关于RBAC的学习资料
    RSS你会用了吗?答曰:不会
    RBAC的资料
  • 原文地址:https://www.cnblogs.com/Jeremy2001/p/7533643.html
Copyright © 2011-2022 走看看