Wednesday, 1 June 2011

Web Service

Web Services can convert your application into a Web-application, which can publish its function or message to the rest of the world.

What are Web Services?

  • Web services are application components
  • Web services communicate using open protocols
  • Web services are self-contained and self-describing
  • Web services can be discovered using UDDI
  • Web services can be used by other applications
  • XML is the basis for Web services

How does it Work?

The basic Web services platform is XML + HTTP.
XML provides a language which can be used between different platforms and programming languages and still express complex messages and functions.
The HTTP protocol is the most used Internet protocol.
Web services platform elements:
  • SOAP (Simple Object Access Protocol)
  • UDDI (Universal Description, Discovery and Integration)
  • WSDL (Web Services Description Language)

A Web Service Example

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. This document is saved as an .asmx file. This is the ASP.NET file extension for XML Web Services.

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

Imports System
Imports System.Web.Services

Public Class TempConvert :Inherits WebService

Public Function FahrenheitToCelsius(ByVal Fahrenheit As String) As String
  dim fahr
  if fahr="" or IsNumeric(fahr)=false then return "Error"
  return ((((fahr) - 32) / 9) * 5)
end function

Public Function CelsiusToFahrenheit(ByVal Celsius As String) As String
  dim cel
  if cel="" or IsNumeric(cel)=false then return "Error"
  return ((((cel) * 9) / 5) + 32)
end function

end class
The only difference from a normal application is that this function is defined as a "WebMethod()".
Use "WebMethod()" to convert the functions in your application into web services.
Publish the .asmx file on a server with .NET support, and you will have your first working Web Service.

No comments:

Post a Comment