How to consume third party webservice in Asp.net

Vuyiswamb
Posted by in ASP.NET category on for Beginner level | Views : 128736 red flag
Rating: 5 out of 5  
 1 vote(s)

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

Page copy protected against web site content infringement by Copyscape

About the Author

Vuyiswamb
Full Name: Vuyiswa Maseko
Member Level: NotApplicable
Member Status: Member,MVP,Administrator
Member Since: 7/6/2008 11:50:44 PM
Country: South Africa
Thank you for posting at Dotnetfunda [Administrator]
http://www.Dotnetfunda.com
Vuyiswa Junius Maseko is a Founder of Vimalsoft (Pty) Ltd (http://www.vimalsoft.com/) and a forum moderator at www.DotnetFunda. Vuyiswa has been developing for 16 years now. his major strength are C# 1.1,2.0,3.0,3.5,4.0,4.5 and vb.net and sql and his interest were in asp.net, c#, Silverlight,wpf,wcf, wwf and now his interests are in Kinect for Windows,Unity 3D. He has been using .net since the beta version of it. Vuyiswa believes that Kinect and Hololen is the next generation of computing.Thanks to people like Chris Maunder (codeproject), Colin Angus Mackay (codeproject), Dave Kreskowiak (Codeproject), Sheo Narayan (.Netfunda),Rajesh Kumar(Microsoft) They have made vuyiswa what he is today.

Login to vote for this post.

Comments or Responses

Posted by: Raja on: 2/10/2010
Wow!!!

Simple yet very nicely explained. Came late but a good start Vuyiswa.

Keep it up!


Login to post response

Comment using Facebook(Author doesn't get notification)