Web Service is a distributed application that provides information to client irrespective of Platform, Languages. There are number of free web services available (webservicex) in universe.
We can call the web service method through JavaScript functions. To doing this, we need to concentrate the following three points,
1.Download Web Service behavior (HTC – HTML Component) file from Microsoft and include that file into your project.
2.Create a Proxy Web service for accessing the original web service (Since, Browser provides security issue while calling the original web service directly.)
3.Create a Page in which, we need to add the code for accessing web service.
For explaining the concept I am using one of the free services which are available in www.webservicex.net. The service is used to validate whether the entered email address is valid one or not.
1.Web Service Behavior
The WebService behavior enables client-side script to invoke remote methods exposed by Web Services, or other Web servers, that support the SOAP and Web Services Description Language (WSDL) 1.1. This behavior provides developers the opportunity to use and leverage SOAP, without requiring expert knowledge of its implementation. The WebService behavior supports the use of a wide variety of data types, including intrinsic SOAP data types, arrays, objects, and XML data. The WebService behavior is implemented with an HTML Component (HTC) file as an attached behavior, so it can be used in Microsoft Internet Explorer 5 and later versions.
For more information click here
You can download the HTC file from Microsoft and include that file into your project.
2.Create a Proxy Web Service
While using the remote web service directly, the browser prompts security issue like,
"This page is accessing information that is not under its control. This poses a security risk "
To avoid this, we need to create proxy service in our local, to access the remote web service.
For creating proxy service we need to do the following steps.
a)Get the particular web service’s WSDL as .cs file and Compile it. Using WSDL.exe command, we can get the .cs file of web service WSDL like,
Go to Visual studio command prompt,
C:\WSDL http://www.webservicex.net/ValidateEmail.asmx?WSDL
The validateemail.cs file will be created. Now we need to compile this file into dll.
C:\csc /t:library /out:WS_ValidateEmail.dll validateemail.cs
The validateemail.dll will be create. Now Copy the dll file and paste into bin folder of your project. Make the reference of this dll via Add reference.
b)Create Proxy Web Service and call the original web service via newly created dll.
Create a proxy web service by click File->New->File and select Web Service. Name the file as “ProxyWS.asmx” and add the following function into the file.
[WebMethod]
public bool IsValidEmail(string email)
{
ValidateEmail objWS = new ValidateEmail();
bool result = objWS.IsValidEmail(email);
return result;
}
3.Create Page to access Web service.
Create a page and add the following scripts.
<script language = "javascript" type ="text/javascript">
function initWS()
{
var email = document.getElementById('txtEmail').value;
if(email.length > 0)
{
// Initialize and create object using web service behavior.
divWS.useService("proxyWS.asmx?WSDL","objWS")
if(divWS.objWS)
{
document.getElementById("sLoad").style.visibility = "visible";
document.getElementById("btnValidate").style.visibility = "hidden";
divWS.objWS.callService(getResult,"IsValidEmail", email);
}
}
}
function getResult(result)
{
if(result.error)
{
alert("Error : " + result.errorDetail.code + " , " + result.errorDetail.string + " , " + result.errorDetail.raw);
}
else
{
if(result.value)
alert("Valid Email");
else
alert("Invalid Email");
}
document.getElementById("sLoad").style.visibility = "hidden";
document.getElementById("btnValidate").style.visibility = "visible";
}
</script>
Add the following code in <body> section,
<form id="form1" runat="server">
<div id = "divWS" style="BEHAVIOR: url(webservice.htc)">
</div>
<table>
<tr>
<td> Enter the Email Address :</td>
<td> <input type = "text" id = "txtEmail" /> </td>
</tr>
<tr>
<td colspan = "2">
<span id = "sLoad" style="visibility:hidden">Loading</span>
<input type = "button" id = "btnValidate" value = "Validate" onclick = "initWS();" />
</td>
</tr>
</table>
</form>
No comments:
Post a Comment