How to Integrate Google Searches into Your Application

Introduction

The first thing that comes to mind when you hear Google is search engine. Google has been able to turn the search business upside down within the last five years. The founders of Google started with an idea in 1995; it really became widely used and known in 1998/99. Today, Google is the number one search engine. You can find out more about Google’s history here. Like other organizations, Google is trying to establish itself as a platform rather then a solution. This means it provides the necessary tools and infrastructure so that other people can build their own solutions on top of it. Google provides a Web service interface that allows you to integrate Google searches right into your application. You can find out more about the Google Web service API at http://www.google.ca/apis.

Getting Started with the Google API

You can download from the URL above the developer’s kit that comes with a number of sample applications for different languages, such as .NET or Java. You also need a valid license key, which you need to pass along with every web service call. To obtain a Google license key, visit http://www.google.ca/apis and select “Create Account” on the left side navigation bar. You need to create an account by entering your e-mail address and a password. This sends an e-mail to the e-mail address you entered to verify its existence. The e-mail you receive has a link to complete the account creation by activating it. When done, click the continue link; it will bring you back to the account creation page. At the bottom of the page you see a link, “sign in here.” Follow the link and sign into your account with your e-mail address and password. This then shows a page confirming that a license key has been generated and sent to your e-mail address. Should you lose your license key, sign in again and Google will resend the license key to your e-mail address. The license key is for free but limits you to 1,000 calls per day. This will be more then enough to get started. If you need to make more then 1,000 calls per day, contact Google.

Referencing the Google Web Service API in Your Project

Create your project in Visual Studio .NET and in the “solution explorer” pane right-click the project. In the popup menu, select “Add Web Reference” and enter the following WSDL URL as the URL—http://api.google.com/GoogleSearch.wsdl. This will check the existence of the WSDL, download it, and show you in the dialog the Web methods available by this Web service. Enter the name of the web service reference under “web reference name;” for example, GoogleSearch. When done, click “Add Reference” and you are ready to use the Google web service API. It will be shown in the “solution explorer” under “Web References.” You can right-click the Web service reference and update it through the “Update Web Reference” menu item or view it in the object explorer through the “View in Object Browser” popup menu. This shows you that there are four different types available. The GoogleSearchService type exposes the actual Web service calls you can make. It has three different Web methods (plus the usual Begin/End methods if you want to call a Web method asynchronously).

GoogleSearchService.doSpellingSuggestion()

When you open Google in your browser and search for a word or phrase, you sometimes see the phrase “Did you mean: [suggested search term]” at the top of the search results page. Google performs a spell check of the search term you entered and then shows you alternative spellings of your search term. This helps the user to search for properly spelled words and phrases and the user can simply click it to search for the corrected search term. The Google Web service also provides a Web method to check for alternate spellings of a search term. Here is a code snippet:

public static string SpellingSuggestion(string Phrase)
{
   // create an instance of the Google Web service
   Google.GoogleSearchService GoogleService = new
      Google.GoogleSearchService();

   // get the new spelling suggestion
   string SpellingSuggestion =
      GoogleService.doSpellingSuggestion(Key, Phrase);

   // null means we have no spelling suggestion
   if (SpellingSuggestion == null)
      SpellingSuggestion = Phrase;

   // release the Web service object
   GoogleService.Dispose();
   return SpellingSuggestion;
}

First, you create an instance of the Web GoogleSearchService class and then you call the doSpellingSuggestion() Web method. The first argument is the Google license key you pass along and the second one is the search term. The Web method returns the alternate spelling of the search term or null if there is no alternate spelling. The code snippet above returns the alternate spelling or the original one. At the end, it calls Dispose() to free up the underlying unmanaged resource.

GoogleSearchService.doGetCachedPage()

Google is constantly crawling the Internet to keep its search index and directory up to date. Google’s crawler also caches the content locally on its servers and allows you to obtain the cached page, which is the content as of when the crawler visited that resource the last time. URLs can point to many different resources, most typically to HTML pages. But, these also can be Word documents, PDF files, PowerPoint slides, and so forth. The cached page is always in HTML format. So, for any other resources than HTML, it also converts the format to HTML. Here is a code snippet:

public static void GetCachedPageAndSaveToFile(string PageUrl,
                                              string FileName)
{
   // create an instance of the Google Web service
   Google.GoogleSearchService GoogleService = new
      Google.GoogleSearchService();

   // get the cached page content
   byte[] CachedPage = GoogleService.doGetCachedPage(Key, PageUrl);

   // file writer to write a stream to the file & a binary writer
   // to write data to
   FileStream FileWriter = new FileStream(FileName, FileMode.Create);
   BinaryWriter Writer   = new BinaryWriter(FileWriter);

   // write the page content to the file and close the streams
   Writer.Write(CachedPage);
   Writer.Close();
   FileWriter.Close();

   // release the Web service object
   GoogleService.Dispose();
}

First, you again create an instance of the GoogleSearchService class and then you call the doGetCachedPage() Web method. You pass along the Google license key plus the URL of the page you are looking for. This returns a byte array, using base64 encoding, which contains the HTML content of the cached page. Next, you create a FileStream that you use to write the obtained page to a local file. With FileMode.Create, you tell it to create the file; this overwrites any existing file. Then, you create a BinaryWriter that uses the FileStream as output. Then, you write the returned byte array to the BinaryWriter which in turn writes it to the FileStream, which in turn writes it to the local file. Then, you close the FileStream and BinaryWriter. At the end, you again call Dispose() to free up underlying unmanaged resources.

More by Author

Get the Free Newsletter!

Subscribe to Developer Insider for top news, trends & analysis

Must Read