|
|
Most telephones and phone lines now support caller-ID, which displays an incoming caller’s name. Sometimes your phone displays only the caller’s phone number.
The GeoPhone web service offered by ServiceObjects lets you determine the person or business that corresponds to a specific phone number. For example, if you enter the phone number 510-523-8233, the service will return the owner, “Sybex Computer Books.”
The ASP.NET page GetCaller.aspx, which you can run from this book’s companion website, displays a form that prompts you to enter a phone number. After you enter the number and click the Get Caller button, the page will use the GeoPhone web service to retrieve the caller information, which the page will then display as shown in Figure 1.12. If the service cannot determine a number’s owner, the page will display a message so stating.
The GeoPhone web service supports the following method:
PhoneInfo GetPhoneInfo(string PhoneNumber, string LicenseKey)
Figure 1.12: Using the GeoPhone web service to retrieve caller information
The GetPhoneInfo method returns a value of type PhoneInfo, which, depending on the phone listing the service discovers, may contain an array of contacts and an array of providers (the telephone companies that manage the numbers). Each entry within the contacts array contains name and address information.
The PhoneNumber parameter that you pass to the GetPhoneInfo method should not contain hyphens. To look up the number 800-555-1212, you would use the string 8005551212.
The GetPhoneInfo method requires that you pass a parameter that specifies your license key. If you visit the ServiceObjects website, you can download a trial key that lets you use the service for a specific period of time. If you need unlimited use of the service, you must purchase a license for service from ServiceObjects. The GetCaller.aspx ASP.NET page uses the license key 0, which provides a limited use of the service. The source code in Listing 1.5 implements the ASP.NET page GetCaller.aspx.
Listing 1.5 GetCaller.aspx
Public Class WebForm1 Inherits System.Web.UI.Page Protected WithEvents Label1 As System.Web.UI.WebControls.Label Protected WithEvents Label2 As System.Web.UI.WebControls.Label Protected WithEvents TextBox1 As System.Web.UI.WebControls.TextBox Protected WithEvents TextBox2 As System.Web.UI.WebControls.TextBox Protected WithEvents TextBox4 As System.Web.UI.WebControls.TextBox Protected WithEvents Button1 As System.Web.UI.WebControls.Button Protected WithEvents TextBox3 As System.Web.UI.WebControls.TextBox #Region " Web Form Designer Generated Code " ' Code not shown #End Region Private Sub Page_Load(ByVal sender As System.Object, ByVal e ÄAs System.EventArgs) Handles MyBase.Load 'Put user code to initialize the page here End Sub Private Sub Button1_Click(ByVal sender As System.Object, ÄByVal e As System.EventArgs) Handles Button1.Click Dim Phone As New net.serviceobjects.ws.GeoPhone() Dim PhoneInfo As net.serviceobjects.ws.PhoneInfo Dim Contact As net.serviceobjects.ws.Contact Dim Provider As net.serviceobjects.ws.Provider Dim PhoneNumber As String Dim ProcessingError As Boolean = False PhoneNumber = TextBox1.Text & TextBox2.Text & TextBox3.Text If (PhoneNumber.Length <> 10) Then TextBox4.Text = "Invalid phone number" & PhoneNumber Else Try PhoneInfo = Phone.GetPhoneInfo(PhoneNumber, 0) Catch Ex As Exception TextBox4.Text = "Error processing number " & Ex.Message ProcessingError = True End Try If (ProcessingError) Or (PhoneInfo Is Nothing) Then TextBox4.Text = "Error processing number" Else TextBox4.Text = "Contact: " If (PhoneInfo.Contacts.Length = 0) Then TextBox4.Text &= "Unknown" Else For Each Contact In PhoneInfo.Contacts TextBox4.Text &= vbCrLf & Contact.Name TextBox4.Text &= vbCrLf & Contact.Address TextBox4.Text &= vbCrLf & Contact.City TextBox4.Text &= " " & Contact.State TextBox4.Text &= " " & Contact.Zip & vbCrLf Next End If End If End If End Sub End Class
Within the button-click event handler, the following statement creates an object the program will use to interact with the GeoPhone web service:
Dim Phone As New net.serviceobjects.ws.GeoPhone()
After the user enters a phone number and clicks the Get Caller button, the code calls the GetPhoneInfo method within a Try-Catch block. If the web service is successful, the code will use a ForEach loop to display each element within the Contacts array.
To build the GetCaller.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 GetCaller. 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.12 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://ws.serviceobjects.net/gp/GeoPhone .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.
|
|