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

WebService - Client手动调用

发布时间:2020-12-16 21:42:22 所属栏目:安全 来源:网络整理
导读:采用的免费webservice接口: http: //ws .webxml .com .cn /WebServices/WeatherWS .asmx ?wsdl 客户端调用代码如下: package com .web .hh .constroller ; import java .util .Iterator ; import org .apache .axiom .om .OMAbstractFactory ; import org

采用的免费webservice接口:

http://ws.webxml.com.cn/WebServices/WeatherWS.asmx?wsdl

客户端调用代码如下:

package com.web.hh.constroller;

import java.util.Iterator;

import org.apache.axiom.om.OMAbstractFactory;
import org.apache.axiom.om.OMElement;
import org.apache.axiom.om.OMFactory;
import org.apache.axiom.om.OMNamespace;
import org.apache.axiom.soap.SOAP11Constants;
import org.apache.axis2.AxisFault;
import org.apache.axis2.Constants;
import org.apache.axis2.addressing.EndpointReference;
import org.apache.axis2.client.Options;
import org.apache.axis2.client.ServiceClient;

public class ClientWeather {
    /* * 第二种方式,手动调用 */
    public static void main(String[] args) throws AxisFault {
        ServiceClient serviceClient = new ServiceClient(); 
        Options option = new Options();
        option.setSoapVersionURI(SOAP11Constants.SOAP_ENVELOPE_NAMESPACE_URI); 
        option.setTransportInProtocol(Constants.TRANSPORT_HTTP); 
        option.setAction("http://WebXml.com.cn/getWeather"); 
        EndpointReference epfs = new EndpointReference("http://ws.webxml.com.cn/WebServices/WeatherWS.asmx?wsdl"); 
        option.setTo(epfs); 
        serviceClient.setOptions(option); 

        OMFactory fac = OMAbstractFactory.getOMFactory(); 
        OMNamespace namespace = fac.createOMNamespace("http://WebXml.com.cn/",""); 
        OMElement element = fac.createOMElement("getWeather",namespace); 
        OMElement theCityCode = fac.createOMElement("theCityCode ",namespace); 
        theCityCode.setText("北京"); 
        element.addChild(theCityCode); 
        OMElement theUserID = fac.createOMElement("theUserID ",namespace); 
        theUserID.setText(""); 
        element.addChild(theUserID); 

        OMElement result = serviceClient.sendReceive(element); 
        System.out.println(result); 
        System.out.println("****************************************************************************************************************");
        Iterator in = result.getChildrenWithLocalName("getWeatherResult"); 
        while(in.hasNext()){  
            OMElement om = (OMElement)in.next(); 
            Iterator in2 = om.getChildElements(); 
            while(in2.hasNext()){  
//                System.out.println(in2.next().toString()); 
                System.out.println(((OMElement)in2.next()).getText()); 
            }  
        }  
    }  
}

输出结果如下:

这里写图片描述

(编辑:李大同)

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

    推荐文章
      热点阅读