1. Home
  2. Computing & Technology
  3. Visual Basic

Writing Your Own Google API Program
How The Code Works

By , About.com Guide

If you've programmed VB .NET, most of the code behind the first form is quite routine. The entire thing is contained in one DoubleClick event code block. The entire program in the main form is shown below for reference. (Lines have been continued to make them shorter for web page display.)

I'm using three Google objects. Documentation can be found for them in the Google API SDK that must be downloaded from the Google site. (See my first article for more on this.) The variable names (GSS, GSR, and GRE) can be changed them to match your application if you wish. The Google objects (more about this just a bit later) are defined when you create a Web Reference to the Google web service. These are Google's names.

Click Here to see an example of the VB .NET Reference Dialog. Click the "Back" button in your browser to return.

GSS - The initial search request that is sent to Google. You must pass an individual Google search key to the web service as a property in this object. Keys are available free of charge from Google, but a key is tied to you as an individual and each key is limited to 1000 searches each day.

GSR - The main "search result" returned from Google. Part of this is a collection of individual "hits".

GRE - An object that holds each individual search hit returned by the search result.

The Google objects are provided "automagically" by VB .NET primarily when a Web Reference is added to the project. To do this, select the Add Web Reference menu item when you left click the project in Solution Explorer. Although you can browse for web services (on your local machine or on the web), in general, you have to know the address. For the Google web service, the address is:

http://api.google.com/GoogleSearch.wsdl

Note the file qualifier, .wsdl - This is the Web Service Description Language. It's an XML file that tells VB .NET exactly what services are available and creates the code you need to use them. Very cool!

Click Here to see an example of the Google Add Web Reference web page. Click the "Back" button in your browser to return.

Many people change the local name of the Web Reference in this window to something shorter for convenience. In this program, we have left it as it is. After you click Add Reference, Solution Explorer shows the service with its unique icon.

Microsoft MSDN describes the capabilities of the Web Service Reference this way:

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.

To see why, you need to look at the new files that were written to your project folder. Using Windows Explorer, look for a folder named Web References in your project. There, you'll see the WSDL XML file (copied from the server), a mapping file (also XML), and a Reference.vb file. The most interesting file is the Reference.vb file. Browse this in NotePad (don't change anything) to see that VB .NET has "autogenerated" a lot of detailed code including the class code for all three objects we're using in this program. You can create this code just by typing it in yourself. But after browsing this file, I think you'll agree that it wouldn't be a lot of fun to do that. "Web services" is a standards driven technology and a Microsoft proprietary method that wasn't compatible with other technologies would be against the rules. So different software companies have their own technologies to create the same SOAP messages that this code generates. This is also one of the big differences between VB 6 and VB .NET

As another exercise, look for the correspondence between the Reference.vb file and the XML information in the WSDL file.

Explore Visual Basic
By Category
About.com Special Features

Holiday Central

What to eat, where to go, fun things to do and how to save money on the perfect gifts. More >

Family Tech Center

Stay connected and entertained with reviews on tips on the latest HDTVs, cellphones and more. More >

  1. Home
  2. Computing & Technology
  3. Visual Basic
  4. Applications
  5. Writing Your Own Google API Program

©2009 About.com, a part of The New York Times Company.

All rights reserved.