zoukankan      html  css  js  c++  java
  • Java 常调用的Webservice接口的方法

      WebService是基于Web的服务,WebService使用SOAP协议实现跨编程语言和跨操作系统平台,接收和响应外部系统的某种请求,从而实现远程调用。WebService采用HTTP协议传输数据,采用XML格式封装数据,SOAP协议=HTTP协议+XML数据格式。主要解决不了不同的系统或者调用分布部署的处理数据项目返回的接口。

      最近自己在项目中,调用webService接口常用到两种方法,自己觉得也比较好用,在这里记录一下。

      第一种,采用httpclient 请求,这种跟我们经常用的HTTP请求一样,结果可以是返回XML格式的字符串,我们比较容易对其进行解析,取得我们想要的数据。

      地址:列如 http://www.xxx.com/WeatherWS/Weather.asmx?wsdl 

      调用的方法是:GetCityForecastByZIP   方法参数是: ZIP

      需要的JAR如下:

      commons-codec-1.4.jar

      commons-httpclient-3.1.jar

      jsoup-1.6.2.jar

      commons-logging-1.0.4.jar

     代码:

              InputStream is = null;
    
          HttpClient client = new HttpClient();
    
    PostMethod method = new PostMethod("www.xxx.com/WeatherWSS/Weather.asmx/GetCityForecastByZIP");
    
    method.setRequestHeader("Host", "www.xxx.com");
    
    method.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
    
    method.setParameter("ZIP", "90001");
    
    try {
    
    client.executeMethod(method);
    
    is = method.getResponseBodyAsStream();
    
    Document document = Jsoup.parse(is,"gbk","");
    
    System.err.println(document);
    
    } catch (Exception e) {
    
    e.printStackTrace();
    
    }finally{
    
    method.releaseConnection();
    
    try {
    
    if(is!=null){
    
    is.close();
    
    }
    
    } catch (IOException e) {
    
    e.printStackTrace();
    
    }
    
    }

     

       我这边 document  是返回XML格式的形式,可以对进行处理,获取我们想要的数据。 

       如图:

           

          

                   

       第二种:采用axis2来调用。这种 axis2通用性不好,有的web服务用axis2不好用。 

      地址:列如 http://www.xxx.com/WeatherWS/Weather.asmx?wsdl  

      调用的方法是:GetCityForecastByZIP   方法参数是: ZIP 

      需要的JAR如下: 

    axis-ant.jar
    axis.jar
    commons-discovery-0.2.jar
    commons-logging-1.0.4.jar 
    jaxrpc.jar 
    wsdl4j-1.5.1.jar

          代码:  

              try{
    
     
    
            // 指出service所在URL       
    
            String endpoint = "http://www.xxx.com/WeatherWS/Weather.asmx";  
    
          String targetNamespace = "http://www.xxx.com/WeatherWS/";
    
             String method="GetCityForecastByZIP";
    
            // 创建一个服务(service)调用(call)       
    
            Service service = new Service();  
    
            Call call = (Call) service.createCall();// 通过service创建call对象       
    
      
    
            // 设置service所在URL       
    
            call.setTargetEndpointAddress(new java.net.URL(endpoint));  
    
            call.setOperationName(new QName(targetNamespace, method));
    
            call.setUseSOAPAction(true);
    
            call.addParameter(new QName(targetNamespace,"ZIP"), org.apache.axis.encoding.XMLType.XSD_STRING,javax.xml.rpc.ParameterMode.IN);//接口的参数
    
            //call.setReturnClass(org.w3c.dom.Element.class); 
    
            call.setReturnType(org.apache.axis.encoding.XMLType.XSD_STRING);//设置返回类型  
    
         //String path = targetNamespace + method;
    
    //call.setSOAPActionURI(path);
    
            Object ret = call.invoke(new Object[] {"90001"});  
    
            System.out.println("xx==="+ret.toString()); 
    
    }catch(Exception e){
    
    e.printStackTrace();
    
    }
    
           

     我比较经常使用httpclient 请求。也可以使用WSDL2JAVA把WSDL文件转为本地的服务类,然后可以直接调用。

  • 相关阅读:
    HDU 4508
    HDU 4503
    HDU 4532
    HDU 4544
    HDU 3167 KMP
    HDU 4542
    HDU 4528
    【70】自然语言处理与词嵌入
    【69】循环神经网络
    【68】一维和三维卷积
  • 原文地址:https://www.cnblogs.com/koal/p/4982762.html
Copyright © 2011-2022 走看看