Part 7 - Real time example of calling live weather forecast web service

Suggested Videos
Part 4 - WebMethod attribute properties
Part 5 - WebMethod overloading in asp.net web services
Part 6 - Calling asp.net web service from javascript using ajax



In this video we will discuss calling a LIVE weather forecast web service. The following is the URL of the real live web service. At the moment, this web service can only be used with US zip codes. There are 3 web methods in this web service. We will use GetCityWeatherByZIP method.
http://wsf.cdyne.com/WeatherWS/Weather.asmx



This web service can be used, if you want to display specific US city weather on your web site. The user interface of the application that we are going to develop, will be as shown below. Once we enter a ZIP code and when we click Get Weather button, we want to invoke the weather web service and display weather information of the provided zip code.
Real time example of calling live weather forecast web service

Here are the steps to consume this web service in an asp.net web application
Step 1: Create a new asp.net empty web application and name it WebServicesDemo.

Step 2: Right click on References folder in the WebServicesDemo project and select Add Service Reference option.

Step 3: In the Address textbox of the Add Service Reference window, type the below web service address and click GO button. In the namespace textbox type WeatherService and click OK.
http://wsf.cdyne.com/WeatherWS/Weather.asmx

Step 4: Right click on WebServicesDemo project in solution explorer and add new webform.

Step 5: Copy and past the following HTML on WebForm1.aspx
<table style="font-family:Arial; border:1px solid black">
    <tr>
        <td>
            <b>Zip Code </b>
        </td>
        <td>
            <asp:TextBox ID="txtZip" runat="server"></asp:TextBox>
            <asp:Button ID="btnGetWeather" runat="server" Text="Get Weather" 
                onclick="btnGetWeather_Click"/>
        </td>
    </tr>
    <tr>
        <td>
            <b>City :</b>
        </td>
        <td>
            <asp:Label ID="lblCity" runat="server"></asp:Label>
        </td>
    </tr>
    <tr>
        <td>
            <b>State :</b>
        </td>
        <td>
            <asp:Label ID="lblState" runat="server"></asp:Label>
        </td>
    </tr>
    <tr>
        <td>
            <b>WeatherStationCity :</b>
        </td>
        <td>
            <asp:Label ID="lblWeatherStationCity" runat="server"></asp:Label>
        </td>
    </tr>
    <tr>
        <td>
            <b>Temperature :</b>
        </td>
        <td>
            <asp:Label ID="lblTemperature" runat="server"></asp:Label>
        </td>
    </tr>
    <tr>
        <td>
            <b>Wind :</b>
        </td>
        <td>
            <asp:Label ID="lblWind" runat="server"></asp:Label>
        </td>
    </tr>
    <tr>
        <td colspan="2">
            <asp:Label ID="lblError" runat="server" Font-Bold="true" ForeColor="Red"></asp:Label>
        </td>
    </tr>
</table>

Step 6: Copy and past the following code on WebForm1.aspx.cs
using System;
namespace WebServicesDemo
{
    public partial class WebForm1 : System.Web.UI.Page
    {
        protected void btnGetWeather_Click(object sender, EventArgs e)
        {
            WeatherService.WeatherSoapClient client = new WeatherService.WeatherSoapClient("WeatherSoap");
            WeatherService.WeatherReturn result = client.GetCityWeatherByZIP(txtZip.Text);

            if (result.Success)
            {
                lblError.Text = "";
                lblCity.Text = result.City;
                lblState.Text = result.State;
                lblTemperature.Text = result.Temperature;
                lblWind.Text = result.Wind;
                lblWeatherStationCity.Text = result.WeatherStationCity;
            }
            else
            {
                lblError.Text = result.ResponseText;
                lblCity.Text = "";
                lblState.Text = "";
                lblTemperature.Text = "";
                lblWind.Text = "";
                lblWeatherStationCity.Text = "";
            }
        }
    }
}

asp.net webservices tutorial

Post a Comment

Previous Post Next Post