WebService
来源:互联网 发布:淘宝哪家买iphone靠谱 编辑:程序博客网 时间:2024/06/06 04:06
采用的免费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()); } } } }
输出结果如下:
【Tips】上面服务是不需要验证的,如果需要验证,参考代码如下:
/** * 为SOAP Header构造验证信息, * 如果你的服务端是没有验证的,那么你不用在Header中增加验证信息 * * @param serviceClient * @param tns 命名空间 * @param user * @param passwrod */ public void addValidation(ServiceClient serviceClient, String tns , String user, String passwrod) { OMFactory fac = OMAbstractFactory.getOMFactory(); OMNamespace omNs = fac.createOMNamespace(tns, "nsl"); OMElement header = fac.createOMElement("AuthenticationToken", omNs); OMElement ome_user = fac.createOMElement("Username", omNs); OMElement ome_pass = fac.createOMElement("Password", omNs); ome_user.setText(user); ome_pass.setText(passwrod); header.addChild(ome_user); header.addChild(ome_pass); serviceClient.addHeader(header); }
阅读全文
0 0
- WebService
- WebService
- webservice
- webservice
- WebService
- webservice
- webService
- WebService
- WebService
- webService
- WEBService
- webService
- WebService
- [WebService]
- webservice
- WebService
- webservice
- WebService
- 打包的msi,在别人电脑找不到网络位置、MFC RC 4005: IDCANCEL:redefinition
- ssm报错之 org.apache.ibatis.binding.BindingException: Invalid bound statement (not found)
- es6从入门到放弃之let,const,块级作用域
- MySQL密码错误,忘记密码无法进入服务,修改密码
- 多行实现文本溢出效果(实用)
- WebService
- 从CSV文件导入Hive出现中文乱码问题解决
- KVM详解,太详细太深入了,经典
- 11.02-HTML的常规使用以及作业
- 嵌入式hi3516-hi3518海思平台移植nginx-1.12.2
- python 常用的一些东西——sys、os 等 (转)
- CentOS 7 yum 安装 Nginx
- 51nod 1174区间中最大的数(线段树)
- enum 在c中的使用