Integrating Google Search using the Google Web Services

This sample shows how to integrate search into your application using the Google Web Service API. I will discuss doing this using VS.NET which makes all this happen like magic and also discuss doing this using the .NET Framework SDK. I have also integrated a browser into the application using the WebBrowser Control. So after doing a search the user can right click on a listview item and use one of the context menu items "Goto Page" or "Cached Page". The Browser menu item "Hide" can be used to hide the browser and go back to the Search Screen. The sample as it stands will not compile. In the file SearchService.cs you need to replace the line

myKey = <REPLACE WITH YOUR GOOGLE KEY>;

with a search key string which you can obtain at http://www.google.com/apis.

Integrating the Web Service

Web Services are described using a WSDL file (which is sort of an equivalent of IDL for web services). The Google WSDL can be obtained from http://api.google.com/GoogleSearch.wsdl. If you are using VS.NET you can just right click on the project and choose "Add Web Reference". In the popup which follows you, type the above URL and once the contract has been loaded you click "Add Reference" and you are done. If you are using the SDK, you can use "wsdl.exe" with this URL as the first argument and it will generate a C# stub for you which you can use in your program. The main methods of interest in the api are

doGoogleSearch(string key, string query, int startIndex, int maxResults, ....., string ie, string oe)
doGetCachedPage(string key, string url)

The arguments which I have marked with "..." allow more tweaking of the search and you can get more information on them at the google site. The last two arguments were menat for input and output encoding, but these are not used anymore and the web service expects input and sends output in UTF-8.
The SearchService class wraps these APIs and increases the max results returned per query. The Google API allows at most 10 results to be returned. This class changes this by making multiple queries.

Integrating the Browser Control

To integrate the browser control in VS.NET, right click on the ToolBox and choose "Customize ToolBox". Under the "COM Components" Tab choose the "Microsoft Web Browser" (SHDocVw.dll). This process will add an IE Icon under "General" in the toolbox. This icon can be dragged and dropped on your form and used like any other control.

Note that for ActiveX components to be used in Forms a wrapper is needed to create a Control apart from the COM interop layer. To do this manually using this SDK, requires a few steps. You run

aximp c:\winnt\system32\shdocvw.dll

This will generate two assemblies one called AxSHDocVw.dll (ActiveX Control Wraper for use in forms) and SHDocVw.dll (interop assembly). The Forms designer seems to generate a lot of code for using the browser, but this is what I found is needed in a small C# program which I wrote.

using System.Windows.Forms;
using System;
using AxSHDocVw;
public class TestForm : Form
{
public AxWebBrowser browser;
public TestForm()
{
browser =
new AxWebBrowser();
browser.BeginInit();
browser.Dock = DockStyle.Fill;
browser.Visible =
true;
this.Controls.Add(browser);
browser.EndInit();
object a = string.Empty;
object url = http://www.news.com;
browser.Navigate2(
ref url, ref a, ref a, ref a, ref a);
}
public static void Main(String[] args)
{
Application.Run(
new TestForm());
}
}

You can compile this program with

csc TestForm.cs /r:AxSHDocVw.dll /r:SHDocVw.dll

I have also integrated some ToolTip text so that when you hover over any ListView item you see a snippet of the page.

Up Next
    Ebook Download
    View all
    Learn
    View all