Shawn Zhang's Programming Zone

软件,时代进步的推动者! 有事儿您Q我(Shawn)!♀飛雪傢銘♀的地盘欢迎您的到来!

导航

把一个 ASP.NET 程序转换为了 Web Services

什么是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 拥有三种基本的元素。

它们是:SOAP(简易对象访问协议)、WSDL(WSDL 是基于 XML 的用于描述 Web Services 以及如何访问 Web Services 的语言) 以及 UDDI(UDDI 是一种目录服务,通过它,企业可注册并搜索 Web services)。

 

任何应用程序都可拥有 Web Service 组件。

Web Services 的创建与编程语言的种类无关。

 

 

 

 

一个实例:ASP.NET Web Service

在这个例子中,我们会使用 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 文件扩展名。

要运行这个例子,我们需要一个 .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。

 

 

使用表单来访问 Web Service

通过使用表单和 HTTP POST,您可以把 web service 置于您的站点上,比如这样:

您可以把 web service 置于您的站点上

您可以使用这些代码把 web service 放置在您的站点上:

<form target="_blank"
action='http://w3school.com.cn/webservices/tempconvert.asmx/FahrenheitToCelsius'
method="POST">
<label>华氏度转换为摄氏度:</label>
<p>
<span>
<input class="frmInput" type="text" size="30" name="Fahrenheit">
</span>
<span>
<input type="submit" value="提交" class="button">
</span>
</p>
</form>
<form target="_blank"
action='http://w3school.com.cn/webservices/tempconvert.asmx/CelsiusToFahrenheit'
method="POST">
<label>摄氏度转换为华氏度:</label>
<p>
<span>
<input class="frmInput" type="text" size="30" name="Celsius">
</span>
<span>
<input type="submit" value="提交" class="button">
</span>
</p>
</form>

posted on 2008-08-01 09:51  Shawn Zhang  阅读(223)  评论(0编辑  收藏  举报