• Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint
Share this Page URL

Chapter 29. XML Web Services > Building the Google Search Web Application

Building the Google Search Web Application

Now that you have the Web service added to your application, it's time to add the code that will consume the Web service. Programming against the Web service requires a bit of knowledge and documentation about the methods that the Web service exposes. Simply looking at the classes doesn't say much about what the Web service actually does. For more information about the Web service, you can navigate to the developer URL that was provided on the xMethods Web site. The URL is www.google.com/apis. Right away you will notice that to use the Web service you will have to create an account, something that you may not have known from adding the Web service from the xMethods Web site. Follow the process of the creating the account. You will need the license key that is generated within the account setup email that is sent to you. After you have the license key, you are ready to begin developing your application. To start building the application, follow the steps outlined next:

Begin by defining the form within the page. Add all necessary elements to use as follows:

Add the form, textbox control, and button control.

<form runat="server">
Search Google: <asp:textbox id="searchText" runat="server"
CssClass="textbox" />
<asp:button id="searchButton" runat="server"
CssClass="textbox" Text="Search" onClick="searchButton_Click" />

Add a panel control so that you can set the visibility of the controls to false or true within it, and add two label controls that will display the result count to the user:

<asp:Panel id="headerPanel" runat="server">
<asp:Label id="lblResults" runat="server" CssClass="body"> Total Results Returned: </asp:Label>&nbsp;
<asp:Label id="lblTotalResults" runat="server"


Add the label that displays the list of searched items. This list includes the link, the description, the URL, and the size:

<asp:label id="resultList" runat="server"></asp:label><br>

Add a Back and Next link button that will be used to navigate between page sets.

<asp:LinkButton id="lbBack" runat="server" Visible="False"
onClick="lbBack_Click">< Back</asp:LinkButton>&nbsp;
<asp:LinkButton id="lbNext" runat="server" Visible="False"
onClick="lbNext_Click">Next ></asp:LinkButton><br>

The result will look similar to Figure 29.19.

Figure 29.19. Add the necessary client code so that the user can interact with the application.

Now add the Import Directive to the top of the page and import the System.Text namespace as follows:

<%@ Import Namespace="System.Text" %>

This namespace will be used to define the string builder object that we will use to format the string that will eventually be passed into the GoogleSearchResult() method as a parameter.

Next, define three global variables to use within the application inside of a code declaration block:

<script runat=""server"">
Public key As String = "YOUR KEY GOES HERE"
Public maxResults As Integer = 10
Public formatString As String = "<font face=arial size=2 color=#336699><a href=""{2}"">{1}</a></font><br><font face=arial size=2>{3}</font>
<br><font face=arial size=2 color=#336699>{2}- {4}</font><br><br>"


  • key Holds the license key given to you when you registered to use the service.

  • maxResults Holds the amount of records to view per page before clicking the next button.

  • formatString Defines the array of elements to use for formatting the string that will be passed into the GoogleSearhResult() method.

It is important to note that all three of these values will eventually be passed into the GoogleSearhResult() method.

Now create a new Page_Load() subroutine as follows:

Private Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)
     If Not Page.IsPostBack Then
          lbNext.Visible = False
          lbBack.Visible = False
          lblResults.Visible = False
          headerPanel.Visible = False
     End If
End Sub

Essentially, you are just checking to see if the page is posting back to itself. If it's not, you are setting the visibility of the preceding controls to false.

Next, add the searchButton_Click() subroutine to the page as follows:

Private Sub searchButton_Click(s As Object, e As EventArgs)
     resultList.Text = doQuery(searchText.Text, 0)
     Session("CurrentRecord") = 0
     headerPanel.Visible = True
End Sub

This subroutine sets the text property of the result list label control equal to the value of a function that you will write next. It also creates a new session variable and sets it to 0. The result of adding the two subroutines will look similar to Figure 29.20.

Figure 29.20. Add a Page_Load() subroutine to handle visibility settings, and then create the button event subroutine to handle the Submit button click.

Next add a new function called doQuery(), passing in two parameters: searchText as String and record as Integer. The result will look similar to the following code:

Function doQuery(searchText As String, record As Integer)
End Function

Inside that function, create new variables for the loop, title, record count, and string builder classes that will be used to format the request string.

Dim i As Integer
Dim displayTitle As String
Dim totalCount As String
Dim sb As StringBuilder = New StringBuilder()

Next, create a new instance of the GoogleSearchService class, and then create a new instance of the GoogleSearchResult() method. Notice that there are 10 parameters that are being passed into the method, including the license key, searchText (formatted using the string builder), record (passed in as a session variable, maxResults (the total number of items to show in the page), and so on:

Dim s As GoogleSearchService = New GoogleSearchService()
Dim r As GoogleSearchResult = s.doGoogleSearch(key, searchText,
record, maxResults, False, "", False, "", "", "")

Now set the estimated total number of results that exist for the query. This is a method called from the GoogleSearchResult class.

totalCount = r.estimatedTotalResultsCount

Next, loop through the array of result elements, including title and the URL associated with that title. This returns a hyperlinked title:

For i = 0 To r.resultElements.GetUpperBound(0)
     If (r.resultElements(i).title <> "") Then
          displayTitle = r.resultElements(i).title
          displayTitle = r.resultElements(i).URL
     End If

Now add code to append displayTitle, URL, snippet, and cachedSize to the array of result items using the string builder object:

sb.Append(String.Format(formatString, (i + 1), displayTitle,
r.resultElements(i).URL, r.resultElements(i).snippet,

Finish the loop.

Next i

Make sure to make the Next LinkButton control visible now that a result is being returned:

lbNext.Visible = True

If there is more than one page in the result, show the Back LinkButton as well, otherwise just show the Next LinkButton:

If (record = 0) Then
     lbBack.Visible = False
     lblResults.Visible = True
     lbBack.Visible = True
     lblResults.Visible = True
End If

Display the total results returned for the query within the label control:

lblTotalResults.Text() = totalCount

Return the value as a string:

doQuery = sb.ToString()

The result will look similar to Figure 29.21.

Figure 29.21. Add the code to the doQuery function.

Your final steps will be to add the next and back functionality. You can add the Next button's click event subroutine by adding the following code:

Private Sub lbNext_Click(s As Object, e As EventArgs)
    Session("CurrentRecord") += +10
resultList.Text = doQuery(searchText.Text,
End Sub

Notice that the Next button adds 10 records to the session variable; this way, the page knows where it's at, at all times. Next you call the doQuery() function, passing in the current record count so that it knows which set of records to display.

The Back button will function similar to the Next button, except the session gets 10 records subtracted from it as follows:

Private Sub lbBack_Click(s As Object, e As EventArgs)
    Session("CurrentRecord") += -10
    resultList.Text = doQuery(searchText.Text, Int32.Parse(Session ("CurrentRecord")))
End Sub


The result of the subroutine additions will resemble Figure 29.22.

Figure 29.22. Add both the Next and Back button subroutines.

Save your work and test it in the browser.



Not a subscriber?

Start A Free Trial

  • Creative Edge
  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint