Web Services 教程——Web Services 实例

来源:互联网 发布:淘宝房产网 编辑:程序博客网 时间:2024/05/17 01:10

本章原文地址:http://www.w3schools.com/webservices/ws_example.asp

 


Web Services Example

Web Services例子


Any application can have a Web Service component.
任何应用程序都可以有一个Web Service组件。

Web Services can be created regardless of programming language.
Web Services的创建与编程语言无关。

---------------------------------------------------------------------

A Web Service Example
一个Web Services例子

In the following example we will use ASP.NET to create a simple Web Service that converts the temperature from Fahrenheit to Celsius, and vice versa:
在以下的例子中我们将使用ASP.NET来创建一个简单的Web Services,它用来实现华氏温度与摄氏温度的相互转化。

<%@ WebService Language="VBScript" Class="TempConvert" %>

 


Imports 
System

Imports System.Web.Services

 

Public Class TempConvert : Inherits WebService

 


<WebMethod()> 
Public Function FahrenheitToCelsius(ByVal Fahrenheit As StringAs String

Dim fahr

fahr = Trim(Replace(Fahrenheit, ",", "."))

Ifahr = "" Or IsNumeric(fahr) = False Then Return "Error"

Return ((((fahr) - 32) / 9) * 5)

End Function

 


<WebMethod()>
 Public Function CelsiusToFahrenheit(ByVal Celsius As StringAs String

Dim cel

cel = Trim(Replace(Celsius, ",", "."))

Icel = "" Or IsNumeric(cel) = False Then Return "Error"

Return ((((cel) * 9) / 5) + 32)

End Function

 


E
nd Class


This document is saved as an .asmx file. This is the ASP.NET file extension for XML Web Services.
另存这份文档为一个.asmx文件,这是ASP.NET中XML Web Services类型文件的扩展名。

---------------------------------------------------------------------

Example Explained
例子解释

Note: To run this example, you will need a .NET server.
注:要运行这个例子,您需要一个.NET服务器。

The first line in the example states that this is a Web Service, written in VBScript, and has the class name "TempConvert":
例子的第一行声明这是一个Web Services,使用VBScript编写,并且它的类名是“TempConvert”:

 

<%@ WebService Language="VBScript" Class="TempConvert" %>


The next lines import the namespace "System.Web.Services" from the .NET framework:
下两行从.NET框架中导入命名空间“System.Web.Services”:

 

Imports System

Imports System.Web.Services


The next line defines that the "TempConvert" class is a WebService class type:
下一行定义了“TempConvert”类是一个WebService类的类型(注:TempConvert类继承了WebService类):

 

Public Class TempConvert : Inherits WebService



The next steps are basic VB programming. This application has two functions. One to convert from Fahrenheit to Celsius, and one to convert from Celsius to Fahrenheit.
下一步是简单的VB编程。这个应用程序有两个函数,一个将华氏温度转化为摄氏温度,另一个是将摄氏温度转化为华氏温度。

The only difference from a normal application is that this function is defined as a "WebMethod()". 
与一般的应用程序唯一不同的是这个函数被定义为一个“WebMethod()”。

Use "WebMethod()" to convert the functions in your application into web services:
使用“WebMethod()”将您应用程序中的函数转化为Web Services:

 

<WebMethod()> Public Function FahrenheitToCelsius(ByVal Fahrenheit As StringAs String

Dim fahr

fahr = Trim(Replace(Fahrenheit, ",", "."))

Ifahr = "" Or IsNumeric(fahr) = False Then Return "Error"

Return ((((fahr) - 32) / 9) * 5)

End Function

<WebMethod()> Public Function CelsiusToFahrenheit(ByVal Celsius As StringAs String

Dim cel

cel = Trim(Replace(Celsius, ",", "."))

Icel = "" Or IsNumeric(cel) = False Then Return "Error"

Return ((((cel) * 9) / 5) + 32)

End Function


Then, end the class:
然后,结束这个类:

End Class


Publish the .asmx file on a server with .NET support, and you will have your first working Web Service.
在使用.NET支持的服务器上发布这个.asmx文件,您将第一次在Web Services上工作。

Look at our 
example Web Service
观看我们的
Web Service实例
(注:
http://www.w3schools.com/webservices/tempconvert.asmx

---------------------------------------------------------------------

ASP.NET Automates the Process
ASP.NET使进程自动化

With ASP.NET, you do not have to write your own WSDL and SOAP documents.
有了ASP.NET,您不需要去编写您自己的WSDL和SOAP文档。

If you look closer at our example Web Service, you will see that ASP.NET has automatically created a WSDL and SOAP request.
如果您仔细观看了Web Service实例,你将发现ASP.NET自动地创建了一个WSDL和SOAP请求。