Web Service学习(一)简介

一、Web Service是什么?

         做项目时,我们经常需要访问第三方的应用,获取我们需要的数据。比如说:我们做个小项目,获取各地的天气信息。我们不会自己去收集天气的信息,工作量太大。而有人会专门做这项工作,然后把天气数据通过Web Service技术发布到网上。这样我们就可以按照规定好的格式去调用他们的方法,获取到我们想要的天气数据。

        所以Web Service就是能使得运行在不同机器上的不同应用无须借助附加的、专门的第三方软件或硬件, 就可相互交换数据或集成

        通俗的讲,Web Service就是一个部署在Web服务器上的一个,它向外界暴露出一个能够通过Web进行调用的API。这就是说,你能够用编程的方法通过Web来调用这个应用程序。我们把调用这个Web Service 的应用程序叫做客户端,发布这个web服务的机应用程序器称为Web Service服务器

二、Web Service优点

访问第三方的应用常用的有Socket和Http两种

       Socket访问  : Socket属于传输层,它是对Tcp/ip协议的实现,包含TCP/UDP,它是所有通信协议的基础,Http协议需要Socket支持,以Socket作为基础。

       Socket通信特点:
      1. 开启端口,该通信是长连接的通信 ,很容易被防火墙拦截,可以通过心跳机制来实现 
      2. 传输的数据一般是字符串 ,可读性不强
                lj|16|1|60|up  
     3. socket端口不便于推广 
         http:17.23.23.2:2345   www.jd.com   www.360buy.com
      4. 性能相对于其他的通信协议是最优的

       Http协议访问 :属于应用层的协议,对Socket进行了封装

      1. 跨平台 
      2. 传数据不够友好 : 
         get请求: http://127.0.0.1:8888?username=lj&pwd=1234
      3. 对第三方应用提供的服务,希望对外暴露服务接口

可以看出,上述两种方式对数据封装都不是很友好,而Web Service则是普通适用标准 :http+xml。

优点总结:

a)异构平台的互通性
理论上, Web Service 最大的优势是提供了异构平台的无缝街接技术手段。由于不同的用户使用不同的硬件平台,不同的操作平台,不同的操作系统,不同的软件,不同的协议通信,这就产生了互相通信的需求。 Web Service 使任何两个应用程序,只要能读写XML,那么就能互相通信。

b)更广泛的软件复用[例如手机淘宝可以复用已有淘宝的业务逻辑.]
软件的复用技术通过组合已有模块来搭建应用程序,能大幅度提高软件的生产效率和质量。用户只要获得了描述 Web Service 的 WSDL 文件,就可以方便地生成客户端代理,并通过代理访问 Web Service 。

c)成本低、可读性强、应用范围广
Web Service 可用基于 XML 的 SOAP 来表示数据和调用请求。并且通过 HTTP 协议传输 XML 格式的数据

d)迅捷的软件发行方式[每个web Service称为一个生产者.不同的生产者可以相互协同合作完成整个应用]
Web Service 将彻底地改变软件的发行方式。软件供应商可以把软件分解成若Web Service 模块构成的系统,直接在 Web 上发布

三、实际用一下第三方的应用

1. http://www.webxml.com.cn/zh_cn/index.aspx

   我们选择下图中的一个天气应用

2.点击上图红框中的,出现如下界面

3. 上面界面往下拉,有一个下图方法

4.点击getWeather,进入一个新页面,并往下拉

有下图一段,注意红框中的网址,我们会用到

5.接下来建个web项目,写个类如下;

package cn.zc.a;

import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;
import java.nio.Buffer;
import org.apache.commons.httpclient.HttpClient;

public class MobileCodeService {

	public void get(String mobileCode,String userID) throws Exception{
		/*     http-get访问第三方应用
		 *    1.  http://    自己手动加
		 *    2.  ws.webxml.com.cn  是从
		 *    http://ws.webxml.com.cn/WebServices/WeatherWS.asmx?op=getWeather
		 *    也是第三方提供的业务类的方法的说明中拿到的
		 *    3. /WebServices/WeatherWS.asmx/getWeather?theCityCode=string&theUserID=string
		 *       也是的
		 *         
		 * */
		URL url = new URL("http://ws.webxml.com.cn/WebServices/WeatherWS.asmx/getWeather?theCityCode="+mobileCode+"&theUserID="+userID);
		HttpURLConnection conn = (HttpURLConnection) url.openConnection();
		conn.setConnectTimeout(5000);
		conn.setRequestMethod("GET");
		// 返回码是200
		if(conn.getResponseCode() == HttpURLConnection.HTTP_OK){  
			InputStream is = conn.getInputStream();
			ByteArrayOutputStream boas = new ByteArrayOutputStream();
			byte[] buffer = new byte[1024];
			int len = -1;
			while((len = is.read(buffer)) != -1){
				boas.write(buffer, 0, len);
			}
			System.out.println(boas.toString());
			boas.close();
			is.close();
			
		}
	}

   
   //post请求方式
	public void post(String mobileCode,String userID) throws Exception{
		HttpClient client = new HttpClient();
		PostMethod postMethod = new PostMethod("http://ws.webxml.com.cn/WebServices/WeatherWS.asmx/getWeather");
		postMethod.setParameter("mobileCode",mobileCode);
		postMethod.setParameter("userID",userID);
		//执行请求,结果码
		int code = client.executeMethod(postMethod);
		//获取结果
		String result = postMethod.getResponseBodyAsString();

	}
	
	//soap请求方式
	public void soap() throws Exception{
		HttpClient client = new HttpClient();
		PostMethod postMethod = new PostMethod("http://ws.webxml.com.cn/WebServices/WeatherWS.asmx/getWeather");
        //设置请求参数
		postMethod.setRequestBody(new FileInputStream("c://soap.xml"));
		postMethod.setRequestHeader("Context-type","text/xml,charset=utf-8");
		//执行请求,结果码
		int code = client.executeMethod(postMethod);
		//获取结果
		String result = postMethod.getResponseBodyAsString();
	}

	public static void main(String[] args) throws Exception {
		MobileCodeService mobileCodeService = new MobileCodeService();
		mobileCodeService.get("北京", "");
		
	}
}

6. 代码中的url就是第4步中红框中的,运行结果

<?xml version="1.0" encoding="utf-8"?>
<ArrayOfString xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://WebXml.com.cn/">
  <string>直辖市 北京</string>
  <string>北京</string>
  <string>792</string>
  <string>2018/12/06 16:53:55</string>
  <string>今日天气实况:气温:-3℃;风向/风力:西北风 4级;湿度:10%</string>
  <string>紫外线强度:弱。空气质量:良。</string>
  <string>紫外线指数:弱,辐射较弱,涂擦SPF12-15、PA+护肤品。
健臻·血糖指数:易波动,风力较大,血糖易波动,注意监测。
穿衣指数:寒冷,建议着厚羽绒服等隆冬服装。
洗车指数:较不宜,风力较大,洗车后会蒙上灰尘。
空气污染指数:良,气象条件有利于空气污染物扩散。
</string>
  <string>12月6日 晴</string>
  <string>-9℃/-2℃</string>
  <string>北风4-5级转3-4级</string>
  <string>0.gif</string>
  <string>0.gif</string>
  <string>12月7日 晴</string>
  <string>-10℃/-4℃</string>
  <string>西北风3-4级转小于3级</string>
  <string>0.gif</string>
  <string>0.gif</string>
  <string>12月8日 晴</string>
  <string>-10℃/-3℃</string>
  <string>西北风转西风小于3级</string>
  <string>0.gif</string>
  <string>0.gif</string>
  <string>12月9日 晴</string>
  <string>-9℃/-2℃</string>
  <string>南风小于3级</string>
  <string>0.gif</string>
  <string>0.gif</string>
  <string>12月10日 多云</string>
  <string>-7℃/0℃</string>
  <string>西风小于3级转北风3-4级</string>
  <string>1.gif</string>
  <string>1.gif</string>
</ArrayOfString>

以上就是我们调用别人发布好的webservice应用。

 

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值