Using Visual Basic .NET to Access Google's Web Service
Learn how to access the Google Web APIs service from a Visual Basic .NET Windows front end. This article describes how to call a SOAP Web service, execute a Google search, access a page in Google's cache, and call the Google spell checker. (5 printed pages)
Web services are some of the most exciting emerging technologies to hit the Internet and business. They allow businesses to connect to each other in a standard manner, performing remote procedure calls thru firewalls using the Internet as a transport. Google has provided one of the first public Web services to allow any developer to access Google's search, cache, and spell check services. Visual Studio .NET makes it simple to access the various functions exposed by the Google Web APIs service.
Figure 1 shows the interface of a simple form. It contains a text box for entering a Google license key (see Sidebar: How to Get the Google Web APIs Toolkit and License Key) and three sections that test three Google functions. The first section allows you to enter search text and returns the number of results. The second section allows you to enter a Web site and return the size of that page in Google's cache. The third section calls the Google spell check function and returns the checked value.
Figure 1: The running form
Now that we've looked at what the form does, let's take a look at the steps involved in coding it. There are four main steps:
- Reference the Web service
- Create the function for doing the search
- Create the function for doing the cache size check
- Create the function for calling the spell check
Referencing the Google Web Service
Here are the steps to reference the Google Web service:
- Create a new Windows Forms project.
- Right-click on the project.
- Select Add Web Reference.
Note The Add Web Reference option is just like the Visual Basic 6 Add Reference, with the exception that instead of having access to all the methods of a COM component, you now have access to an XML Web service that is sitting on a different network. Better yet, this access is fully typed, and Intellisense is triggered just like any local object.
- Type the location of the web service description (http://api.google.com/GoogleSearch.wsdl) in the Address text box.
- Click on the Add Reference button to import the Web service definition.
- Once it is imported, go to the Solution Explorer and open the Web References node on the tree.
- Rename the reference to Google by right-clicking on it and picking Rename.
- Draw the form shown above.
Add the code to each button. Double-clicking on each button (just like Visual Basic 6) provides access to the method that is run when the click event fires. Let's take a look at each of these in turn.
Performing a Google Search
Looking at the code of the btnSearch_Click method in Listing 1, we can see that a new GoogleSearchService object was created. In the next line, a new GoogleSearchResult object was created and the new Visual Basic .NET capability of declaring and creating the object in one line was used. In Visual Basic .NET, the two following snippets are equivalent:
Dim x As String = "Hello"
Dim x As String x = "Hello"
After the result object (created by passing a Google license key, search text, and other parameters to the search object) is created, the estimatedTotalResultsCount property is used to fill the label. In four lines of code, a procedure across the web was called, a Google search was performed, and the number of results was posted.
This example is a simple use of the search service. To learn more about the parameters to the search request and the data returned, see the reference documentation from Google.
' Create a Google Search object. Dim s As New Google.GoogleSearchService() ' Invoke the search method. Dim r As Google.GoogleSearchResult = s.doGoogleSearch(txtLicenseKey.Text, txtSearchTerm.Text, 0, 1, _ False, "", False, "", "", "") ' Extract the estimated number of results for the search and display it Dim estResults As Integer = r.estimatedTotalResultsCount lblSearchResults.Text = CStr(estResults)
Listing 1 – The btnSearch_Click method
Getting the Size of a Web Page
As you can see in Listing 2, getting the size of a cached web page on the Google servers is easy. In this case, the doGetCachedPage method was called, and assigned to a byte array. Then that array is asked for its length.
' Create a Google Search object Dim s As New Google.GoogleSearchService() ' Invoke the doGetCachedPage method and get the cached bytes Dim bytes() As System.Byte = _ s.doGetCachedPage(txtLicenseKey.Text, txtCachePage.Text) ' Display the length of the cached page lblCacheResults.Text = CStr(bytes.Length)
Listing 2 – the btnCache_Click method
Doing a Spell Check
The final Google method calls the spell check functionality of the Google engine. The doSpellingSuggestion method on the GoogleSearchService object is called, and if we have results, we simply display them.
' Create a Google Search object Dim s As New Google.GoogleSearchService() ' Ask for spelling suggestion Dim suggestion As String = _ s.doSpellingSuggestion(txtLicenseKey.Text, txtSpell.Text) ' Display the suggestion, if any If suggestion Is Nothing Then lblSpellResults.Text = "<no suggestion>" Else lblSpellResults.Text = suggestion End If
Listing 3 – the btnSpell_Click method
The downloaded code wraps each of the functions in a Try…Catch block (see Listing 4). This block allows us to trap any errors coming back from the SOAP Web service and displays it in a MsgBox. The most common error that you will receive happens when you forget to include your own license key. To get a license key, see How to Get the Kit and a License Key.
' Create a Google Search object Dim s As New Google.GoogleSearchService() Try ' Invoke the doGetCachedPage method and get the cached bytes Dim bytes() As System.Byte = _ s.doGetCachedPage(txtLicenseKey.Text, txtCachePage.Text) ' Display the length of the cached page lblCacheResults.Text = CStr(bytes.Length) Catch ex As System.Web.Services.Protocols.SoapException MsgBox(ex.Message) End Try
Listing 4 – the btnCache_Click method with exception handling
Visual Basic .NET provides an easy way to access Web services on the Web. Adding this capability to your "bag of tricks" can provide new types of capabilities – just like adding COM objects and ActiveX controls added new capabilities to your Visual Basic 6 applications. You could also try adding Google's spell check abilities to your Web-based message board or auto-monitoring the Web for new information on a particular issue. For more ideas, you can also search the Web (try search terms "VB Google API applications") to find applications other VISUAL BASIC developers have done using Google Web APIs. Have fun!
To access the Google Web APIs toolkit and to request a license key, go to http://www.google.com/apis/ and follow the steps. You can download the developer's kit and create your own Google account, which after some e-mail verification, provides you with a license that can be used for 1,000 free queries per day.