Programming the Google Web API with VB.NET

Create GUI and console Google search applications with Visual Basic and the .NET framework.

Along with the functionally identical C# [Hack #58] version, the Google Web APIs Developer’s Kit [Section 5.4] (dotnet/Visual Basic folder) includes a sample Google search in Visual Basic. While you can probably glean just about all you need from the Google Demo Form.vb code, this hack provides basic code for a simple console Google search application without the possibile opacity of a full-blown Visual Studio .NET project.

Tip

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.

The Code

' googly.vb
' A Google Web API VB.NET console application
' Usage: googly.exe <query>
' Copyright (c) 2002, Chris Sells.
' No warranties extended. Use at your own risk.
Imports System
Module Googly
  Sub Main(ByVal args As String(  ))
    ' Your Google API developer's key
    Dim googleKey As String = "insert key here"
    ' Take the query from the command-line
    If args.Length <> 1 Then
      Console.WriteLine("Usage: google.exe <query>")
      Return
    End If
    Dim query As String = args(0)
    ' Create a Google SOAP client proxy, generated by:
    ' c:> wsdl.exe /l:vb http://api.google.com/GoogleSearch.wsdl
    Dim googleSearch As GoogleSearchService = New GoogleSearchService(  )
    ' Query Google
    Dim results As GoogleSearchResult = googleSearch.
doGoogleSearch(googleKey, query, 0, 10, False, "", False, "", "latin1", 
"latin1")
    ' No results?
    If results.resultElements Is Nothing Then Return
    ' Loop through results
    Dim result As ResultElement
    For Each result In results.resultElements
      Console.WriteLine(  )
      Console.WriteLine(result.title)
      Console.WriteLine(result.URL)
      Console.WriteLine(result.snippet)
      Console.WriteLine(  )
    Next
  End Sub
End Module

You’ll need to replace "insert key here" with your Google developer’s key Section 5.5 (e.g., 12BuCK13mY5h0E/34KN0cK@ttH3Do0R). Your code should look something like:

' Your Google API developer's key
Dim googleKey As String = "12BuCK13mY5h0E/34KN0cK@ttH3Do0R"

Compiling the Code

Before compiling the VB application 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 Section 5.6 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 to do all the coding for you.

Tip

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 and specify that you’d like Visual Basic proxy code:

C:GOOGLY.NETVB>wsdl.exe /l:vb 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.NETVB>wsdl.exe /l:vb 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.NETVBGoogleSearchService.vb'.

What you get is a GoogleSearchService.vb file with all that underlying Google SOAP-handling ready to go:

'---------------------------------------------------------------------------
' <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>
'---------------------------------------------------------------------------
Option Strict Off
Option Explicit On
Imports System
Imports System.ComponentModel
Imports System.Diagnostics
Imports System.Web.Services
Imports System.Web.Services.Protocols
Imports System.Xml.Serialization
...
    Public Function BegindoGoogleSearch(ByVal key As String, ByVal q As
String, ByVal start As Integer, ByVal maxResults As Integer, ByVal
filter As Boolean, ByVal restrict As String, ByVal safeSearch As
Boolean, ByVal lr As String, ByVal ie As String, ByVal oe As String,
ByVal callback As System.AsyncCallback, ByVal asyncState As Object) As
System.IAsyncResult
        Return Me.BeginInvoke("doGoogleSearch", New Object(  ) {key, q, 
start, maxResults, filter, restrict, safeSearch, lr, ie, oe}, callback,
asyncState) End Function
    
    '<remarks/>
    Public Function EnddoGoogleSearch(ByVal asyncResult As System.
IAsyncResult) As GoogleSearchResult
        Dim results(  ) As Object = Me.EndInvoke(asyncResult)
        Return CType(results(0),GoogleSearchResult)
    End Function
End Class
...

Now to compile that googly.vb:

C:GOOGLY.NETVB>vbc /out:googly.exe *.vb
Microsoft (R) Visual Basic .NET Compiler version 7.00.9466
for Microsoft (R) .NET Framework version 1.00.3705
Copyright (C) Microsoft Corporation 1987-2001. All rights reserved.

Running the Hack

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

C:GOOGLY.NETVB>googly.exe "query words"

Tip

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

The Results

Functionally identical to its C# counterpart [Hack #58], running this Visual Basic hack should turn up about the same results—Google index willing.

—Chris Sells and Rael Dornfest

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.129.148.210