When you think ASP, think...
Recent Articles xml
All Articles
ASP.NET Articles
Related Web Technologies
User Tips!
Coding Tips
spgif spgif

Book Reviews
Sample Chapters
JavaScript Tutorials
MSDN Communities Hub
Official Docs
Stump the SQL Guru!
Web Hosts
Author an Article
spgif spgif

ASP ASP.NET ASP FAQs Feedback topnav-right
Print this Page!
Published: Wednesday, March 5, 2003

Searching Google Using the Google Web Service

By Scott Mitchell


Did you know that Google provides a Web service for searching through Google's database, retrieving cached versions of Web pages, and performing spelling checks? Using Google's Web service you can provide Google's search functionality on your own Web site. Over the next month or so I plan on authoring two to three articles describing how to utilize Google's Web services. In this first article, we'll look at how to use the Web service to search through Google's database.

- continued -

Licensing Terms of the Google Web Service
The Google Web Service API is currently in Beta testing, and is only available for personal use. To limit excessive use, Google requires that those who wish to use the Google Web service acquire a unique license key (which is free to obtain). This license key is used to limit individuals to no more than 1,000 calls to the Google Web service per day. Please be sure to read the license terms.

A Quick Primer on Web Services

A Web service is an external interface provided by a Web site that can be called from other Web sites. Think of Web services as a self-contained component with one or more methods. This component can reside anywhere on the Internet, and can have its methods invoked by remote clients. For example, the Google Web service provides three methods: doGoogleSearch(), doGetCachedPage(), and doSpellingSuggestion(). The doGoogleSearch(), which we'll be examining in this article, has a number of input parameters that specify the search query. The method then returns an instance of the GoogleSearchResult object, which has the results of the search.

Web services are built on open protocols and standards. For example, the communication between a client that wishes to consume a Web service, and the Web service itself, happens over HTTP, a well-known, open protocol. The parameters and return values being passed back and forth are packaged using SOAP, a well-known, open protocol for data-marshalling. The relevant point here is that Web services can be exposed on, say, a Microsoft IIS Web server and be consumed by PHP Web pages running on Apache, by ASP.NET Web pages running on IIS 6.0, or even by a desktop application.

When consuming a Web service, typically a proxy class is created to shield the client from the complexity involved in invoking the Web service. A proxy class is a class that itself contains all of the methods and objects that the Web service exposes. These methods, when called from the client program, handle the marshalling of the parameters into SOAP, sending the SOAP request over HTTP, receiving the response from the Web service, and unmarshalling the return value. The proxy class allows the client program to call a Web service as if the Web service was a local component.

If you are unfamiliar with Web services, this primer serves as a good introduction, but you should definitely take the time to read Creating a Web Service and then Creating and Consuming a Web Service.

The Google Web Service API

The Google Web Service information can be found online at http://www.google.com/apis/. To start using the Google Web Service you will first need to download the Google Web API Developer's Kit. This 666K file includes the WSDL (Web Service Description Language) file that fully describes the Web service, and examples of accessing the Google Web Service in both Java and VB.NET/C#.

After downloading the Google Web API Developer's Kit, you will need to create an account with Google. This can be done at: https://www.google.com/accounts/NewAccount?continue=http://api.google.com/createkey. Once you create one of these free accounts, you will be assigned a unique license number. This license number must be used whenever a Google Web service method is called. The purpose of this license is to limit the number of calls to the Google Web service to 1,000 invocations per license key per day.

Creating the Proxy Class

Once you have a license key and the Google API Developer's Kit, the next step is to create the proxy class that we'll use to call the Web service. To accomplish this, we first need to get our hands on the WSDL file, which is an XML-formatted file that describes the services provided by the Google Web service. This WSDL file, GoogleSearch.wsdl is located in the Google Web API Developer's Kit.

If you are using Visual Studio .NET, copy this file to the ASP.NET Web directory (like C:\Inetpub\wwwroot\WebApplication1). Then, in Visual Studio .NET, go to the Project menu and select the Add Web Reference option. Then, in the dialog box, enter the URL to the WSDL file, which will look like: http://localhost/WebApplication1/GoogleSearch.wsdl (see the screenshot to the right). To complete the process, click the Add Reference button. This will create the proxy class using the namespace localhost (which you can change if you like).

If you do not have Visual Studio .NET, you can create the proxy class through a command-line program called wsdl.exe. Wsdl.exe will create a C# or VB.NET file, which you'll then need to compile. To run wsdl.exe, drop to the command-line and enter:

wsdl /protocol:SOAP /namespace:google /out:GoogleProxy.cs C:\google\GoogleSearch.wsdl

This will create a C# file named GoogleProxy.cs with the namespace google. To compile this class, use the C# command-line compiler, csc, like so:

csc /t:library /out:GoogleProxy.dll GoogleProxy.cs

This will create a file named GoogleProxy.dll. Be sure to copy this file to your Web application's /bin directory!

For More Information on Wsdl.exe
For more information on creating a proxy class without using Visual Studio .NET, be sure to read the PowerPoint presentation: Calling a Web Service from an ASP.NET Web Page.

Creating an ASP.NET Web Page that Calls the Google Web Service

Now that we have created the proxy class, calling the Google Web Service through an ASP.NET Web page is a breeze. Before we examine how, precisely, to do this, we need to first examine what parameters the Web service methods expect. Fortunately, these methods and their input parameters are detailed in the reference section on Google's Web site. Since, in this article, we'll focus on simply performing a search via the Google Web services, let's examine the parameters for the doGoogleSearch() method.

This method takes in 10 parameters:

Provided by Google, this is required for you to access the Google service. Google uses the key for authentication and logging.
(See Query Terms section for details on query syntax.)
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. (See Automatic Filtering section for more details.)
Restricts the search to a subset of the Google Web index, such as a country like "Ukraine" or a topic like "Linux." (See Restricts for more details.)
A Boolean value which enables filtering of adult content in the search results. See SafeSearch for more details.
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. (See Input and Output Encodings section for details.)
Output Encoding - this parameter has been deprecated and is ignored. All requests to the APIs should be made with UTF-8 encoding. (See Input and Output Encodings for details.)

The doGoogleSearch() method returns an instance of the GoogleSearchResult object. This object has a resultElements property, which is an array of ResultElement objects. Each ResultElement object has a number of properties, such as title, snippet, URL, summary, and so on.

Now, let's create a simple ASP.NET Web page that will display the first 10 search results for the search query ASP. This can be accomplished using the following code:

<script language="VB" runat="server">
  Sub Page_Load(sender as Object, e as EventArgs)
    Dim search As google.GoogleSearchService = New google.GoogleSearchService()
    Dim result as google.GoogleSearchResult
    result = search.doGoogleSearch("license key...", "ASP", _
                              0, 10, True, "", True, "", "", "")
    dlResults.DataSource = result.resultElements
  End Sub

<asp:DataList runat="server" id="dlResults"
      Font-Name="Verdana" Font-Size="10pt">
    <span style="font-size:15pt;font-weight:bold;">
      <%# Container.DataItem.title %>
    </span><br />
    <%# Container.DataItem.summary %><br />
    [<a href="<%# Container.DataItem.URL %>">
        <%# Container.DataItem.URL %>
    <p> </p>
[View a Live Demo!]

The bolded text shows the code necessary to call the Google Web service's doGoogleSearch() method. Such little code is needed thanks to the proxy class. The search results are displayed in a DataList, with each result displaying the title, summary, and the URL to access the page.

While the previous live demo illustrates how to call the Google Web service to perform a search, it is fairly limited in that it only displays the first 10 records of a predefined search query. In Part 2 we'll see how to create a more useful ASP.NET Web page that employs the Google search Web service.

  • Read Part 2!

  • ASP.NET [1.x] [2.0] | ASPMessageboard.com | ASPFAQs.com | Advertise | Feedback | Author an Article