Chinaunix首页 | 论坛 | 博客
  • 博客访问: 2030152
  • 博文数量: 414
  • 博客积分: 10312
  • 博客等级: 上将
  • 技术积分: 4921
  • 用 户 组: 普通用户
  • 注册时间: 2007-10-31 01:49
文章分类

全部博文(414)

文章存档

2011年(1)

2010年(29)

2009年(82)

2008年(301)

2007年(1)

分类: Java

2008-09-01 00:19:17

任何应用程序都可拥有 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

Public Function FahrenheitToCelsius
(ByVal Fahrenheit As Int16) As Int16
Dim celsius As Int16
celsius = ((((Fahrenheit) - 32) / 9) * 5)
Return celsius
End Function

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" 来标记函数。

 Public Function FahrenheitToCelsius
(ByVal Fahrenheit As Int16) As Int16
Dim celsius As Int16
celsius = ((((Fahrenheit) - 32) / 9) * 5)
Return celsius
End Function

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 的自动化处理

通过 ASP.NET,不必亲自编写 WSDL 和 SOAP 文档。

如果仔细研究这个例子,您会发现 ASP.NET 会自动创建 WSDL 和 SOAP 请求。

阅读(1152) | 评论(0) | 转发(0) |
0

上一篇:Why Web Services?

下一篇:Web Service 使用

给主人留下些什么吧!~~