Web Service相关基础概念整理

今早西安很冷,冷的让人有点感觉像是世界末日。

整理下WebService方面的知识吧,顺便给项目添加些天气预报、时间校正等功能,其实是实在不想写业务内容相关的代码了。


好,开始,从一道面试题开始:

问:WEB SERVICE 名词解释。JSWDL 开发包的介绍。JAXP 、JAXM 的解释。SOAP 、UDDI,WSDL 解释。 

答:Web ServiceWeb Service是基于网络的、分布式的模块化组件,它执行特定的任务,遵
守具体的技术规范,这些规范使得Web  Service能与其他兼容的组件进行互操作。  
JAXP(Java API for XML Parsing)  定义了在 Java 中使用 DOM, SAX, XSLT 的通用的接口。
这样在你的程序中你只要使用这些通用的接口,当你需要改变具体的实现时候也不需
要修改代码。  
JAXM(Java API fo r XML Messaging)  是为SOAP 通信提供访问方法和传输机制的API。  
WSDL 是一种 XML  格式,用于将网络服务描述为一组端点,这些端点对包含面向文档
信息或面向过程信息的消息进行操作。这种格式首先对操作和消息进行抽象描述,然
后将其绑定到具体的网络协议和消息格式上以定义端点。相关的具体端点即组合成为
抽象端点(服务)。  
SOAP 即简单对象访问协议(Simple Object Access Protocol),它是用于交换XML编码信
息的轻量级协议。  
UDDI  的目的是为电子商务建立标准;UDDI 是一套基于Web 的、分布式的、为 Web  
Service 提供的、信息注册中心的实现标准规范,同时也包含一组使企业能将自身提供
的Web  Service注册,以使别的企业能够发现的访问协议的实现标准。 

有些名词,需要再理解一下:

Web Services 平台元素:SOAP、WSDL 以及 UDDI。

什么是 SOAP?
基本的 Web services 平台是 XML + HTTP。

•SOAP 指简易对象访问协议
•SOAP 是一种通信协议
•SOAP 用于应用程序之间的通信
•SOAP 是一种用于发送消息的格式
•SOAP 被设计用来通过因特网进行通信
•SOAP 独立于平台
•SOAP 独立于语言
•SOAP 基于 XML
•SOAP 很简单并可扩展
•SOAP 允许您绕过防火墙
•SOAP 将作为 W3C 标准来发展

什么是 WSDL?
WSDL 是基于 XML 的用于描述 Web Services 以及如何访问 Web Services 的语言。

•WSDL 指网络服务描述语言
•WSDL 使用 XML 编写
•WSDL 是一种 XML 文档
•WSDL 用于描述网络服务
•WSDL 也可用于定位网络服务
•WSDL 还不是 W3C 标准

什么是UDDI?
UDDI 是一种目录服务,通过它,企业可注册并搜索 Web services。

•UDDI 指通用的描述、发现以及整合(Universal Description, Discovery and Integration)。
•UDDI 是一种用于存储有关 web services 的信息的目录。
•UDDI 是一种由 WSDL 描述的网络服务接口目录。
•UDDI 经由 SOAP 进行通迅。
•UDDI 被构建于 Microsoft .NET 平台之中。

有点很空虚的感觉,实现一个例子,可能就懂什么是WebService,看源码:

package dec;

import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.List;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.w3c.dom.DOMException;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

/**
 * 
 * 
 * <p>
 * Title: 天气预报测试代码 /p>
 * 
 * <p>
 * Description: 示例 业务类
 * </p>
 * 
 * <p>
 * Copyright: Copyright (c) 2012
 * </p>
 * 
 * @author dml@2012-12-10
 * @version 1.0
 */

public class WeatherReport {
	private static String SERVICES_HOST = "www.webxml.com.cn";
	private static String WEATHER_SERVICES_URL = "http://webservice.webxml.com.cn/WebServices/WeatherWS.asmx/";
	private static String PROVINCE_CODE_URL = WEATHER_SERVICES_URL
			+ "getRegionProvince";
	private static String CITY_CODE_URL = WEATHER_SERVICES_URL
			+ "getSupportCityString?theRegionCode=";
	private static String WEATHER_QUERY_URL = WEATHER_SERVICES_URL
			+ "getWeather?theUserID=&theCityCode=";

	private WeatherReport() {
	}

	public static void main(String[] args) throws Exception {
		int provinceCode = getProvinceCode("陕西"); 
		int cityCode = getCityCode(provinceCode, "西安"); 
		List<String> weatherList = getWeather(cityCode);
		for (String weather : weatherList) {
			System.out.println(weather);
			System.out.println("=========");
		}
	}

	public static int getProvinceCode(String provinceName) {
		Document document;
		DocumentBuilderFactory documentBF = DocumentBuilderFactory
				.newInstance();
		documentBF.setNamespaceAware(true);
		int provinceCode = 0;
		try {
			DocumentBuilder documentB = documentBF.newDocumentBuilder();
			InputStream inputStream = getSoapInputStream(PROVINCE_CODE_URL); // 具体webService相关
			document = documentB.parse(inputStream);
			NodeList nodeList = document.getElementsByTagName("string"); // 具体webService相关
			int len = nodeList.getLength();
			for (int i = 0; i < len; i++) {
				Node n = nodeList.item(i);
				String result = n.getFirstChild().getNodeValue();
				String[] address = result.split(",");
				String pName = address[0];
				String pCode = address[1];
				if (pName.equalsIgnoreCase(provinceName)) {
					provinceCode = Integer.parseInt(pCode);
				}
			}
			inputStream.close();
		} catch (DOMException e) {
			e.printStackTrace();
		} catch (ParserConfigurationException e) {
			e.printStackTrace();
		} catch (SAXException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return provinceCode;
	}

	public static int getCityCode(int provinceCode, String cityName) {
		Document doc;
		DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
		dbf.setNamespaceAware(true);
		int cityCode = 0;
		try {
			DocumentBuilder db = dbf.newDocumentBuilder();
			InputStream is = getSoapInputStream(CITY_CODE_URL + provinceCode); // 具体webService相关
			doc = db.parse(is);
			NodeList nl = doc.getElementsByTagName("string"); // 具体webService相关
			int len = nl.getLength();
			for (int i = 0; i < len; i++) {
				Node n = nl.item(i);
				String result = n.getFirstChild().getNodeValue();
				String[] address = result.split(",");
				String cName = address[0];
				String cCode = address[1];
				if (cName.equalsIgnoreCase(cityName)) {
					cityCode = Integer.parseInt(cCode);
				}
			}
			is.close();
		} catch (DOMException e) {
			e.printStackTrace();
		} catch (ParserConfigurationException e) {
			e.printStackTrace();
		} catch (SAXException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return cityCode;
	}

	public static InputStream getSoapInputStream(String url) {
		InputStream inputStream = null;
		try {
			URL urlObj = new URL(url);
			URLConnection urlConn = urlObj.openConnection();
			urlConn.setRequestProperty("Host", SERVICES_HOST); // 具体webService相关
			urlConn.connect();
			inputStream = urlConn.getInputStream();
		} catch (MalformedURLException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return inputStream;
	}

	public static List<String> getWeather(int cityCode) {
		List<String> weatherList = new ArrayList<String>();
		Document document;
		DocumentBuilderFactory documentBF = DocumentBuilderFactory
				.newInstance();
		documentBF.setNamespaceAware(true);
		try {
			DocumentBuilder documentB = documentBF.newDocumentBuilder();
			InputStream inputStream = getSoapInputStream(WEATHER_QUERY_URL
					+ cityCode);
			document = documentB.parse(inputStream);
			NodeList nl = document.getElementsByTagName("string");
			int len = nl.getLength();
			for (int i = 0; i < len; i++) {
				Node n = nl.item(i);
				String weather = n.getFirstChild().getNodeValue();
				weatherList.add(weather);
			}
			inputStream.close();
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		} catch (DOMException e) {
			e.printStackTrace();
		} catch (ParserConfigurationException e) {
			e.printStackTrace();
		} catch (SAXException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return weatherList;
	}

}

执行结果:

陕西 西安
=========
西安
=========
1344
=========
2012/12/10 10:41:37
=========
今日天气实况:气温:1℃;风向/风力:西风 1级;湿度:64%
=========
空气质量:较差;紫外线强度:中等
=========
穿衣指数:天气凉,建议着厚外套加毛衣等春秋服装。体弱者宜着大衣、呢外套。因昼夜温差较大,注意增减衣服。
过敏指数:天气条件极不易诱发过敏,可放心外出,享受生活。
运动指数:天气较好,无雨水困扰,较适宜进行各种运动,但因气温较低,在户外运动请注意增减衣物。
洗车指数:适宜洗车,未来持续两天无雨天气较好,适合擦洗汽车,蓝天白云、风和日丽将伴您的车子连日洁净。
晾晒指数:天气不错,午后温暖的阳光仍能满足你驱潮消霉杀菌的晾晒需求。
旅游指数:天气晴朗,风和日丽,温度适宜,是个好天气哦。这样的天气很适宜旅游,您可以尽情地享受大自然的风光。
路况指数:天气较好,路面比较干燥,路况较好。
舒适度指数:天气较好,早晚会感觉偏凉,午后舒适、宜人。
空气污染指数:气象条件较不利于空气污染物稀释、扩散和清除,请适当减少室外活动时间。
紫外线指数:属中等强度紫外线辐射天气,外出时建议涂擦SPF高于15、PA+的防晒护肤品,戴帽子、太阳镜。
=========
12月10日 晴
=========
-1℃/10℃
=========
西风微风转东风微风
=========
0.gif
=========
0.gif
=========
12月11日 多云
=========
0℃/7℃
=========
东风微风转西风微风
=========
1.gif
=========
1.gif
=========
12月12日 阴转小雨
=========
0℃/5℃
=========
西南风微风转东风微风
=========
2.gif
=========
7.gif
=========
12月13日 雨夹雪转阴
=========
0℃/4℃
=========
东风微风
=========
6.gif
=========
2.gif
=========
12月14日 阴
=========
1℃/5℃
=========
东风微风
=========
2.gif
=========
2.gif
=========

这下应该懂了吧,www.webxml.com.cn站上提供了很多服务,股票查询,时间校正,数据同步等等等等。

自己动手试试,项目里可以增添的功能有很多。

能够做到这点之后,就该动手自己写WebService了,需要掌握WSDL和SOAP,当然,相关的知识暨源码详解会单独写篇博客,先就这么多吧。


p.s.学会如何调用Webservice服务之后,还需要掌握如何自行编写WS程序,这个,可参考图文详解java编写webservice程序

pp.s.“懦怯囚禁人的灵魂,希望可以感受自由。强者自救,圣者渡人。”


dml@2012.12.10

  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值