Whether you are a frequent traveler or you are finally getting away for a much-needed vacation, you can use the AirportInfo web service offered by Web ServiceX.Net to determine specifics about an airport, such as airport’s longitude and latitude, the length of the airport runway, and so on.
The ASP.NET page at AirportInfo.aspx at this book’s companion website prompts a user to enter an airport code, such as JFK or LAX. After the user specifies the airport and clicks the Airport Information button, the page will display current conditions at the airport, as shown in Figure 1.14. Behind the scenes, web services make extensive use of XML-based data. Within the .NET environment, your programs normally do not have to work directly with the XML. The AirportInfo web service returns a string that contains its result in XML. For simplicity and to introduce you briefly to XML-based data, the AirportInfo.aspx ASP.NET page displays the service’s result using an XML-based format. In later chapters, you will learn how to parse XML-based documents to extract the specific data you need.
Figure 1.14: Using the AirportInfo web service to retrieve XML-based airport information
The AirportInfo web service supports myriad methods that your programs can call to get airport information by airport code, city, state, and more. The AirportInfo.aspx page uses the getAirportInformationbyAirportCode method:
string getAirportInformationbyAirportCode(string Code)
The source code in Listing 1.7 implements the ASP.NET page AirportInfo.aspx.
Listing 1.7 AirportInfo.aspx
Public Class WebForm1 Inherits System.Web.UI.Page Protected WithEvents Label1 As System.Web.UI.WebControls.Label Protected WithEvents TextBox1 As System.Web.UI.WebControls.TextBox Protected WithEvents Button1 As System.Web.UI.WebControls.Button Protected WithEvents TextBox2 As System.Web.UI.WebControls.TextBox Protected WithEvents Image1 As System.Web.UI.WebControls.Image #Region " Web Form Designer Generated Code " ' Code not shown. #End Region Private Sub Button1_Click(ByVal sender As System.Object, _ ÄByVal e As System.EventArgs) Handles Button1.Click Dim AirportInfo As New net.webservicex.www.AirportInfoWebservice() Dim Info As String Dim WebError As Boolean = False Try Info = AirportInfo.getAirportInformationByAirportCode(TextBox1.Text) Catch Ex As Exception WebError = True TextBox2.Text = "Web service error: " & Ex.Message End Try If (Not WebError) Then TextBox2.Text = "" Dim I As Integer Dim Letter As String For I = 0 To Info.Length - 1 Letter = Info.Substring(I, 1) If (Letter = ">") Then TextBox2.Text &= Letter & vbCrLf Else TextBox2.Text &= Letter End If Next End If End Sub End Class
Within the button-click event handler, the following statement creates an object the code uses to interact with the web service:
Dim AirportInfo As New net.webservicex.www.AirportInfoWebservice()
Then, the program calls the service, passing to the method the airport code the user specifies. If the service is successful, the code then displays the result within the text box. To create the AirportInfo.aspx ASP.NET page, perform these steps:
Within Visual Studio .NET, select the File menu New Project option. Visual Studio .NET will display the New Project dialog box.
Within the New Project dialog box Project Types list, click Visual Basic Projects. Then, within the Templates field, click ASP.NET Web Application. Finally, within the Location field, specify the name AirportInfo. Select OK. Visual Studio .NET will display a page onto which you can drag and drop the program’s controls (label, buttons, and text box).
Using the Toolbox, drag and drop the label, buttons, and text boxes previously shown in Figure 1.14 onto the page. Using the Properties window, set the text box to support multiline operations.
Select the Project menu Add Web Reference option. Visual Studio .NET will display the Add Web Reference dialog box.
Within the Address field, type the URL http://www.webservicex.net/airport.asmx?wsdl and press Enter. The dialog box will load the file’s contents. Click the Add Reference button.
Select the View menu Code option. Visual Studio .NET will display the program’s source code. Enter the program statements previously shown.