A lot of newbie’s think of Web services as one complicated beast. I have seen Programmers who know OOP well be failed to understand web services. In this Article i will explain the usage of Web services. There are a lot sites that provide free web services, like Bible Search services, Weather Services, SMS sending services and more. You can look for one of the following services in the following site
Introduction
A lot of newbie’s think of Web services as one
complicated beast. I have seen Programmers who know OOP well be failed to
understand web services. In this Article i will explain the usage of Web services. There are a lot sites that provide free web services, like Bible
Search services, Weather Services, SMS sending services and more. You can look
for one of the following services in the following site
Background
In
this Article I am going explain how to create your own date picker with the
Controls you have on your Visual Studio tool.
Using the Code
We are going to user C# as our
language.
Start
Open Visual Studio and Create a New Website.
Automatically you will have an empty page defined for you like this
<% @ Page
Language ="C#"
AutoEventWireup ="true" CodeFile ="Default.aspx.cs" Inherits ="_Default" %>
<! DOCTYPE html PUBLIC
"-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
< html xmlns ="http://www.w3.org/1999/xhtml">
< head runat ="server">
< title ></ title >
</ head >
< body >
< form id ="form1" runat ="server">
< div >
</ div >
</ form >
</ body >
</ html >
Go to Design View and you will notice
there is nothing on your page. Now open your Toolbox and add a Label Control and
a button. And after you have added these two Controls you should have something
like this in your mark-up.
<% @ Page
Language ="C#"
AutoEventWireup ="true" CodeFile ="Default.aspx.cs" Inherits ="_Default" %>
<! DOCTYPE html PUBLIC
"-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
< html xmlns ="http://www.w3.org/1999/xhtml"
>
< head runat ="server">
< title > Untitled
Page</ title >
</ head >
< body >
< form id ="form1" runat ="server">
< div >
< asp : Label ID ="Label1" runat ="server"></ asp : Label >
< br />
< br />
< asp : Button ID ="Button1" Text
="Get Web Service" runat ="server"
/>
</ div >
</ form >
</ body >
</ html >
The Next step is to consume the
External Web Service. The Process is the same, just that now you need to get
the webservice address from the third party. After get the address. Right click
on your web Project and select the option as depicted in the following diagram
After you have selected the option you
will be taken to another dialog box
In the URL part that is where you need
to enter the address from the Third party provider of the webservice and click the
green button. If the service really exists and there is nothing wrong with it.
It will show you the Following
Now the external webservice has been
found and it has exposed a function named “HelloWorld”. In the Web reference
name change it to be a small one world that can be easily accessed will rename
the “ www.VuyiswaMaseko.com ” to “www” and click ok and in your Solution
explorer the following will be added.
Now it’s time to Access the Function in
our Web Application. Remember we have a Button. Now double click the button on
your design view and you will be taken to the Click even of the Button and
write the Following Code.
protected void
Button1_Click(object sender, EventArgs e)
{
www.Service
obj = new www.Service ();
try
{
Label1.Text = obj.HelloWorld();
}
catch (ApplicationException ex)
{
Label1.Text = ex.Message;
}
finally
{
obj = null ;
}
}
Now what we are doing here is we a re
creating an Object of a class that is being exposed by the webserviceand
accessing its funtions. A web service is like a Class that is in your project
accessed via objects. But the Difference is that the webservice is in the
different location. Next step is to run your web application and Click on the
Button and the Following should happen
The “Hello World” is a coming from the
external webservice. Think of Webservices as local classes that has functions
that are ready to be used, but accessed from anywhere.
Conclusion
The
Solution was simple and clear. This is how we consume third party and local
webservice.
Thank
you for visiting DotnetFunda.
Vuyiswa
Maseko
If you like this article, subscribe to our RSS Feed . You can also subscribe via email to our Interview Questions, Codes and Forums section.