Web Services 可以将应用程序转换为网络应用程序。
通过使用 Web Services,您的应用程序可以向全世界发布信息,或提供某项功能。
Web Services 可以被其他应用程序使用。
通过 Web Services,您的会计部门的 Win 2k 服务器可以与 IT 供应商的 UXIX 服务器相连接。
基本的 Web Services 平台是 XML+HTTP。
Web services 使用 XML 来编解码数据,并使用 SOAP 来传输数据。
Web Services 可使您的应用程序成为 Web 应用程序。
Web Services 通过 Web 进行发布、查找和使用。
什么是Web Services?- Web Services 是应用程序组件
- Web Services 使用开放协议进行通信
- Web Services 是独立的(self-contained)并可自我描述
- Web Services 可通过使用UDDI来发现
- Web Services 可被其他应用程序使用
- XML 是 Web Services 的基础
基础的 Web Services 平台是 XML + HTTP。
HTTP 协议是最常用的因特网协议。
XML 提供了一种可用于不同的平台和编程语言之间的语言。
Web services 平台的元素:- SOAP (简易对象访问协议)
- UDDI (通用描述、发现及整合)
- WSDL (Web services 描述语言)
我们会在本教程后面章节讲解这些主题。
Web services 的未来请不要过早地期待太多!
Web services 平台是简单的可共同操作的消息收发框架。它仍然缺少许多诸如安全和路由等重要的特性。但是,一旦 SOAP 变得更加高级,这些事项就会得到解决。
Web services 有望使应用程序更加容易通信。
Web services 把 Web 应用程序提升到了另外一个层面通过使用 Web services,您的应用程序可向全世界发布功能或消息。
Web services 使用 XML 来编解码数据,并使用 SOAP 借由开放的协议来传输数据。
通过 Web services,您的会计部门的 Win 2k 服务器可与 IT 供应商的 UNIX 服务器进行连接。
Web services 有两种类型的应用 可重复使用的应用程序组件有一些功能是不同的应用程序常常会用到的。那么为什么要周而复始地开发它们呢?
Web services 可以把应用程序组件作为服务来提供,比如汇率转换、天气预报或者甚至是语言翻译等等。
比较理想的情况是,每种应用程序组件只有一个最优秀的版本,这样任何人都可以在其应用程序中使用它。
连接现有的软件通过为不同的应用程序提供一种链接其数据的途径,Web services有助于解决协同工作的问题。
通过使用 Web services,您可以在不同的应用程序与平台之间来交换数据。
任何应用程序都可拥有 Web Service 组件。
Web Services 的创建与编程语言的种类无关。
在这个例子中,我们会使用 ASP.NET 来创建一个简单的 Web Service。
<%@ WebService Language="VB" Class="TempConvert" %> Imports System Imports System.Web.Services Public Class TempConvert :Inherits WebService <WebMethod()> Public Function FahrenheitToCelsius (ByVal Fahrenheit As Int16) As Int16 Dim celsius As Int16 celsius = ((((Fahrenheit) - 32) / 9) * 5) Return celsius End Function <WebMethod()> Public Function CelsiusToFahrenheit (ByVal Celsius As Int16) As Int16 Dim fahrenheit As Int16 fahrenheit = ((((Celsius) * 9) / 5) + 32) Return fahrenheit End Function End Class此文档是一个 .asmx 文件。这是用于 XML Web Services 的 ASP.NET 文件扩展名。
此文档中第一行表明这是一个 Web Service,由 VB 编写,其 class 名称是 "TempConvert"。
<%@ WebService Language="VB" Class="TempConvert" %>接下来的代码行从 .NET 框架导入了命名空间 "System.Web.Services"。
Imports System Imports System.Web.Services下面这一行定义 "TempConvert" 类是一个 WebSerivce 类:
Public Class TempConvert :Inherits WebService接下来的步骤是基础的 VB 编程。此应用程序有两个函数。一个把华氏度转换为摄氏度,而另一个把摄氏度转换为华氏度。
与普通的应用程序唯一的不同是,此函数被定义为 "WebMethod"。
请在您希望其成为 web services 的应用程序中使用 "WebMethod" 来标记函数。
<WebMethod()> Public Function FahrenheitToCelsius (ByVal Fahrenheit As Int16) As Int16 Dim celsius As Int16 celsius = ((((Fahrenheit) - 32) / 9) * 5) Return celsius End Function <WebMethod()> Public Function CelsiusToFahrenheit (ByVal Celsius As Int16) As Int16 Dim fahrenheit As Int16 fahrenheit = ((((Celsius) * 9) / 5) + 32) Return fahrenheit End Function最后要做的事情是终止函数和类:
End Function End Class假如您把此文件另存为 .asmx 文件,并发布于支持 .NET 的服务器上,那么您就拥有了第一个可工作的 Web Service。
通过 ASP.NET,你不必亲自编写 WSDL 和 SOAP 文档。
如果您仔细研究我们的这个例子,您会发现 ASP.NET 会自动创建 WSDL 和 SOAP 请求。
本测试使用 HTTP POST,会发送类似这样的 XML 响应:
<?xml version="1.0" encoding="utf-8" ?> <short xmlns="http://tempuri.org/">38</short>通过使用表单和 HTTP POST,您可以把 web service 置于您的站点上,比如这样:
华氏度转换为摄氏度:摄氏度转换为华氏度:
您可以使用这些代码把 web service 放置在您的站点上:
<form target="_blank" action='http://w3school.com.cn/webservices/tempconvert.asmx/FahrenheitToCelsius' method="POST"> 华氏度转换为摄氏度:<input class="frmInput" type="text" size="30" name="Fahrenheit"> <input type="submit" value="提交" class="button">
<form target="_blank" action='http://w3school.com.cn/webservices/tempconvert.asmx/CelsiusToFahrenheit' method="POST"> 摄氏度转换为华氏度:<input class="frmInput" type="text" size="30" name="Celsius"> <input type="submit" value="提交" class="button">