Google Hacks Free Open Book

Google Hacks

Previous Section Next Section

Hack 58 Programming the Google Web API with C# and .NET

figs/moderate.giffigs/hack58.gif

Create GUI and console Google search applications with C# and the .NET framework.

The Google Web APIs Developer's Kit Section 5.4 includes a sample C# Visual Studio .NET (http://msdn.microsoft.com/vstudio/) project for a simple GUI Google search application (take a look in the dotnet/CSharp folder). The functional bits you'd probably find most interesting are in the Form1.cs code.

This hack provides basic code for a simple console Google search application similar in function (and, in the case of Java [Hack #56], form, too) to those in Perl [Hack #50], Python [Hack #57], et al.

Compiling and running this hack requires that you have the .NET Framework (http://msdn.microsoft.com/library/default.asp?url=/nhp/default.asp?contentid=28000519) installed.

58.1 The Code

// googly.cs
// A Google Web API C# console application
// Usage: googly.exe <query>
// Copyright (c) 2002, Chris Sells.
// No warranties extended. Use at your own risk.
using System;
class Googly {
  static void Main(string[] args) {
    // Your Google API developer's key
    string googleKey = "insert key here";
    // Take the query from the command-line
    if( args.Length != 1 ) {
      Console.WriteLine("Usage: google.exe <query>");
      return;
    }
    string query = args[0];
    // Create a Google SOAP client proxy, generated by:
    // c:\> wsdl.exe http://api.google.com/GoogleSearch.wsdl
    GoogleSearchService googleSearch = new GoogleSearchService(  );
    // Query Google
    GoogleSearchResult results = googleSearch.doGoogleSearch(googleKey, 
query, 0, 10, false, "", false, "", "latin1", "latin1");
    // No results?
    if( results.resultElements == null ) return;
    // Loop through results
    foreach( ResultElement result in results.resultElements ) {
      Console.WriteLine(  );
      Console.WriteLine(result.title);
      Console.WriteLine(result.URL);
      Console.WriteLine(result.snippet);
      Console.WriteLine(  );
    }
  }
}

Remember to insert your Google developer's key Section 5.5 (e.g., 12BuCK13mY5h0E/34KN0cK@ttH3Do0R) in place of "insert key here":

// Your Google API developer's key
string googleKey = "12BuCK13mY5h0E/34KN0cK@ttH3Do0R";

58.2 Compiling the Code

Before compiling the C# code itself, you must create a Google SOAP client proxy. The proxy is a wodge of code custom built to the specifications of the GoogleSearch.wsdl file, an XML-based description of the Google Web Service, all its methods, parameters, and return values. Thankfully, you don't have to do this by hand; the .NET Framework kit includes an application, wsdl.exe, that does all the coding for you.

This is a remarkable bit of magic if you think about it: the lion's share of interfacing to a web service autogenerated from a description thereof.

Call wsdl.exe with the location of your GoogleSearch.wsdl file like so:

C:\GOOGLY.NET>wsdl.exe GoogleSearch.wsdl

If you don't happen to have the WSDL file handy, don't fret. You can point wsdl.exe at its location on Google's web site:

C:\GOOGLY.NET\CS>wsdl.exe http://api.google.com/GoogleSearch.wsdl
Microsoft (R) Web Services Description Language Utility
[Microsoft (R) .NET Framework, Version 1.0.3705.0]
Copyright (C) Microsoft Corporation 1998-2001. All rights reserved.
Writing file 'C:\GOOGLY.NET\CS\GoogleSearchService.cs'.

The end result is a GoogleSearchService.cs file that looks something like:

//--------------------------------------------------------------------------
// <autogenerated>
//     This code was generated by a tool.
//     Runtime Version: 1.0.3705.288
//
//     Changes to this file may cause incorrect behavior and will be lost if 
//     the code is regenerated.
// </autogenerated>
//--------------------------------------------------------------------------
// 
// This source code was auto-generated by wsdl, Version=1.0.3705.288.
// 
using System.Diagnostics;
using System.Xml.Serialization;
using System;
using System.Web.Services.Protocols;
using System.ComponentModel;
using System.Web.Services;
...
    public System.IAsyncResult BegindoGoogleSearch(string key, 
    string q, int start, int maxResults, bool filter, string restrict,
    bool safeSearch, string lr, string ie, string oe, 
    System.AsyncCallback callback, object asyncState) {
        return this.BeginInvoke("doGoogleSearch", new object[] {
                    key,
                    q,
                    start,
                    maxResults,
                    filter,
                    restrict,
                    safeSearch,
                    lr,
                    ie,
                    oe}, callback, asyncState);
    }
...

Now on to googly.cs itself:

C:\GOOGLY.NET\CS>csc /out:googly.exe *.cs
Microsoft (R) Visual C# .NET Compiler version 7.00.9466
for Microsoft (R) .NET Framework version 1.0.3705
Copyright (C) Microsoft Corporation 2001. All rights reserved.

58.3 Running the Hack

Run Googly on the command line, passing it your Google query:

C:\GOOGLY.NET\CS>googly.exe "query words"

The DOS command window isn't the best at displaying and allowing scrollback of lots of output. To send the results of your Google query to a file for perusal in your favorite text editor, append: > results.txt.

58.4 The Results

% googly.exe "WSDL while you work"
Axis/Radio interop, actual and potential
http://www.intertwingly.net/stories/2002/02/08/
axisradioInteropActualAndPotential.html <b>...</b> But
<b>you</b> might find more exciting services here
<b>...</b> Instead, we should <b>work</b>
together and<br> continuously strive to <b>...</b>
<b>While</b> <b>WSDL</b> is certainly far from
perfect and has many <b>...</b>  
...
Simplified <b>WSDL</b>
http://capescience.capeclear.com/articles/simplifiedWSDL/
<b>...</b> So how does it <b>work</b>?
<b>...</b> If <b>you</b> would like to edit
<b>WSDL</b> <b>while</b> still avoiding<br> all
those XML tags, check out the <b>WSDL</b> Editor in
CapeStudio. <b>...</b> 

—Chris Sells and Rael Dornfest

    Previous Section Next Section


         Main Menu
    Main Page
    Table of content
    Copyright
    Dedication
    Credits
    Foreword
    Preface
    Chapter 1. Searching Google
    Chapter 2. Google Special Services and Collections
    Chapter 3. Third-Party Google Services
    Chapter 4. Non-API Google Applications
    Chapter 5. Introducing the Google Web API
    5.1 Hacks #50-59
    5.2 Why an API?
    5.3 Signing Up and Google's Terms
    5.4 The Google Web APIs Developer's Kit
    5.5 Using the Key in a Hack
    5.6 What's WSDL?
    5.7 Understanding the Google API Query
    5.8 Understanding the Google API Response
    Hack 50 Programming the Google Web API with Perl
    Hack 51 Looping Around the 10-Result Limit
    Hack 52 The SOAP::Lite Perl Module
    Hack 53 Plain Old XML, a SOAP::Lite Alternative
    Hack 54 NoXML, Another SOAP::Lite Alternative
    Hack 55 Programming the Google Web API with PHP
    Hack 56 Programming the Google Web API with Java
    Hack 57 Programming the Google Web API with Python
    Hack 58 Programming the Google Web API with C# and .NET
    Hack 59 Programming the Google Web API with VB.NET
    Chapter 6. Google Web API Applications
    Chapter 7. Google Pranks and Games
    Chapter 8. The Webmaster Side of Google
    Colophon
    Index


    More Books
    PHP Hacks
    Processing Xml With Java - A Guide To Sax, Dom, Jdom, Jaxp, And Trax
    The Koran (Holy Qur'an)
    Macromedia Flash 8 Bible
    Search Engine Optimization for Dummies
    YouTube Traffic
    PHP 5 for Dummies
    Harry Potter and The Chamber of Secrets
    Harry Potter and the Sorcerer's Stone
    The Pilgrim's Progress
    Wireless Hacks
    Flash Hacks. 100 Industrial-Strength Tips & Tools
    PayPal Hacks. 100 Industrial-Strength Tips and Tools
    Amazon Hacks
    Pdf Hacks
    The Da Vinci Code
    Google Hacks
    The Holy Bible
    Windows XP For Dummies
    Harry Potter and the Half-Blood Prince
    Seo Book
    Upgrading and Repairing Networks
    Macromedia Dreamweaver 8 UNLEASHED
    Windows XP Annoyances
    Windows XP Hacks
    Microsoft Windows XP Power Toolkit
    Teach Yourself MS Office In 24Hours
    iPod & iTunes Missing Manual
    PC Hacks 100 Industrial-Strength Tips and Tools
    PC Overclocking, Optimization, and Tuning - 2th Edition
    PC Hardware In A Nutshell 3rd Edition
    PC Hardware in a Nutshell, 2nd Edition
    Upgrading and Repairing PCs
    Google for Dummies
    MySQL Cookbook
    Teach Yourself Macromedia Flash 8 In 24 Hours
    PHP CookBook
    Sams Teach Yourself JavaScript in 24 Hours
    PHP5 Manual
    Free Games Paper Airplanes
    500 Juegos Gratis 500 Giochi Gratis 500 Jeux Gratuits 500 Jogos Gratis 500 Kostenlose Spiele