Obtaining a License Key
The best part of using the Google Web service API is that it is free to use in accordance with the terms and limitations listed in the section titled Terms and Limitations. You'll find it extremely simple and quick to obtain a license key. The license key issued by Google must accompany all searches processed.
|With a developer license you can only make a maximum of 1,000 searches per day. You must obtain a developer license key from Google prior to using their Web service.|
To obtain a license key, register with Google to create a new account in step 2 at http://www.google.com/apis/index.html. Once you've created a new account, Google will e-mail the license key to the email address entered.
Referencing the Web Service
Once you have your license key, you need to configure your application to consume the Google Web service. In Visual Studio .NET you add a Web Reference to the WSDL document. Remember that your WSDL document describes how to interact with the Web service and what functionality the Web service exposes.
To consume the Google Web service, start Visual Studio .NET and open the project where you want to use the Google Web service. Right-click on the project in the Solution Explorer and select Add Web Reference.
When the Add Web Reference dialog box appears, enter http://api.google.com/GoogleSearch.wsdl in the URL dropdown and click Go. If your application cannot locate the Web service, verify that the Google Web service API is online. Also verify that you have adequate access to the Web from your development machine. Figure 3
shows the Add Web Reference dialog box with the Google Web service located online. Once your application has located the Web service, enter an optional name for the Web service and click Add Reference. A folder named Web References should appear in the project folder structure with the newly added Google Web service listed inside of it.
Calling the Web Service
|Figure 3. The Add Web Reference dialog box with the Google Web service located.|
Once you've added a Web Reference for the Google Web service, the functionality exposed by the Web service should be available to your code. The Web service will be referenced by prefixing it with the root namespace name for the project that the Web Reference was added to. The Google Web service exposes three methods: doGetCachedPage
, and doGoogleSearch
Use the doGetCachedPage
method to submit a URL to the Google Web service and receive the contents of the URL that the Google database has cached. The Google search engine crawls the Web gathering new content and pages, and indexes them in the Google database. When the search engine encounters a URL, it takes a snapshot of the contents of the URL and stores the snapshot in the database. Thus, the contents returned by the doGetCachedPage
method will only be as recent as the last time that the Google search engine encountered the URL requested. An identifying key and the URL are passed to the method as strings and the resulting content is returned as System.Byte
Base64 encoded text.
You'll use the doSpellingSuggestion
method to create the same signature functionality found on the Google home search page. When you perform a search, Google lists a possible spelling correction suggestion at the top of the page so that if a user misspells a key search term, the search engine will offer the correct spelling of the term. The doSpellingSuggestion
accepts a word or phrase containing up to 10 individual words and 2048 bytes and it returns a text string with the suggested spelling term.
method is the heart and soul of the Google Web service API. It performs the actual search functionality based on supplied search criteria. Table 1
lists the possible arguments that you can pass to the doGoogleSearch
method. The key argument is the license key that Google issued. You must include it in any search requests made to the Google Web service. The q
argument is the term that you want Google to search.
Table 1. The arguments required by the doGoogleSearch function member.
Provided by Google, you must use this license key to access the Google service. Google uses the key for authentication and logging.
The query term that Google should search for.
Zero-based index of the first desired result.
Number of results desired per query. The maximum value per query is 10. Note: If you do a query that doesn't have many matches, the actual number of results you get may be smaller than what you request.
Activates or deactivates automatic results filtering, which hides very similar results and results that all come from the same Web host. Filtering tends to improve the end user experience on Google, but for your application you may prefer to turn it off.
Restricts the search to a subset of the Google Web index, such as a country like "Ukraine" or a topic like "Linux."
A Boolean value that enables filtering of adult content in the search results.
Language Restrict: Restricts the search to documents within one or more languages.
Input Encoding: This parameter has been deprecated and is ignored. All requests to the APIs should be made with UTF-8 encoding.
Output Encoding: This parameter has been deprecated and is ignored. All requests to the APIs should be made with UTF-8 encoding.
In my example project for this article I've incorporated the Google Web service into an ASP.NET page. My application consumes the Google Web service directly in the code behind page. To call the Google Web service, my application first creates an instance of the GoogleSearchService object as shown in this code snippet:
// Declare a new instance of the Google Search
// Service object.
Google.GoogleSearchService googleSearchService =
This code creates an instance of the GoogleSearchService using an identifier called googleSearchService
. Next the application submits a search to the Web service by calling the doGoogleSearch
, it pass the appropriate arguments, and then stores the results into a new instance of the GoogleSearchResult object. The snippet below illustrates the call to the doGoogleSearch
// Declare a new instance of the Google Search
// Result object to hold the results of the query.
Google.GoogleSearchResult googleSearchResult =
startRange, numberResults, false,"",true,"","","");
In this code snippet, I create a new instance of the GoogleSearchResult object using an identifier of googleSearchResult
. GoogleSearchResult will hold a collection of search results returned by the GoogleSearchService.doGoogleSearch
function member. The snippet above also illustrates the call made to the doGoogleSearch
function member with the appropriate arguments. You can see the license key as the first argument. My application pulls the query terms from a textbox named txtSearchCriteria
on the ASP.NET page. It passes a variable named startRange
as the starting row number to be returned by the search, and it passes a variable named numberResults
as the number of results for the search to return.
As a convenience to the user, you can display the total number of results found as well as many other attributes of the search. The snippet below illustrates acquiring the estimated total number of results found by the search.
// Nab the number of matches for the search.
int estimatedCount =
Earlier I mentioned that with your developer license, you can return a maximum of 10 results per search. If you want to combine the total number of results found with the arguments of the doGoogleSearch
function member, the 10 results returned by a search do not have to be the first 10 results found. Your search could return the second 10 results or 5 results starting with the 30th result found. In fact, you could return more than 10 results for a single query term as long as your application performs multiple searches using the same query term. For instance, if a user submitted a query term that resulted in 48 matches, the first search could pull up 10 results from 0 to 9 (keep in mind that the result count is zero based). If the user chose to see results 10 through 19, your application could perform a second search using the same query term but requesting results 10 through 19.