Accessing Google's Web services using Visual Basic. NET

Source: Internet
Author: User
Tags definition contains exception handling soap reference web services visual studio
google|visual|web| access to Google Web Services using Visual Basic. NET

Learn how to access Google's Web API service from the Visual Basic. NET Windows Front-End. This article describes how to invoke a SOAP Web service, perform a Google search, access a page in the Google cache, and call Google's spelling checker.

From MSDN Code Center (http://msdn.microsoft.com/downloads/sample.asp?url=/msdn-files/026/002/863/MsdnCompositeDoc.xml). (English) Download VBGoogleDemo.exe

Download sample (Http://download.microsoft.com/download/VisualStudioNET/doc_samp/7.0/NT5XP/EN-US/VBGoogleDemo.exe)

Directory
Web Services really cool
Sample form
Process
A note on exception handling
Summary
How to obtain a toolkit and license key
Web Services really cool
Web Services (English) is in the ascendant, the most exciting technology that is having a huge impact on the Internet and business. WEB services allow you to conduct your business in a regular manner, while using the Internet as a transport tool to perform remote procedure calls through firewalls. Google is one of the pioneers in providing public Web services, allowing any developer to access Google's search, caching, and spell checking services. Visual Studio. NET simplifies the process of accessing the various features of the Google Web API service.

Sample form
Figure 1 shows the interface of the sample form. It contains a text box for entering Google license keys (see Supplemental Instructions "How to obtain Google WEB API toolkit and License Keys"), and includes three areas for testing three Google features. Enter the search text in the first area to return the number of results. Entering a Web site in the second zone returns the size of the page in the Google cache. The Google spell check feature is invoked in the third area, and the check value is returned.



Figure 1: The Running form

Process
Once you understand the functionality of a form, let's take a look at the steps to encode the form. There are four main steps involved:

Referencing Web Services
Create a function to perform a search
To create a feature that checks the cache size
Create a feature that calls the spelling checker
Referencing Google Web Services
The steps to refer to Google Web services are as follows:

Create a new Windows Forms (Windows Forms) project.
Right-click the item.
Select Add Web Reference (Add Web Reference).
Note: The Add Web Reference option is similar to the Add Reference in Visual Basic 6, except that you now have access to XML Web services located on different networks rather than to the CO All methods of the M component. The advantage is that the type of access is OK and can trigger IntelliSense just like any local object.
In the Address text box, type the location of the Web service description (HTTP://API.GOOGLE.COM/GOOGLESEARCH.WSDL).
Click the Add Reference button to import the Web service definition.
After you import the definition, go to Solution Explorer (Solution Explorer), and then open the Web References (Web Reference) node on the tree.
Right-click the Google Reference and select Rename (rename) to rename the reference.
Drag the form shown above.
Add code for each button. Double-click each button (as in Visual Basic 6) To access the method that runs when the click event is triggered. Let's look at each button in turn.

Perform Google search
Looking at the code for the btnSearch_Click method in Listing 1, we can see that a new Googlesearchservice object has been created. In the next line, you create a new Googlesearchresult object and use the new Visual Basic. NET features that declare and create objects on a single line. In Visual Basic. NET, the following two pieces of code are equivalent:

Dim x as String = "Hello"
And

Dim x as String
x = "Hello"
After creating a result object (created by passing the Google license key, search text, and other parameters to the search object), use the Estimatedtotalresultscount property to fill in the label. In four lines of code, a WEB-scoped process is invoked, a Google search is executed, and the number of results is sent.

This example is a simple illustration of how to use the search service. For more information about search request parameters and return data, refer to Google's reference documentation.

' Create a Google search object.
Dim s as New Google.googlesearchservice ()
' Call the search method.
Dim r as Google.googlesearchresult =
S.dogooglesearch (Txtlicensekey.text, Txtsearchterm.text, 0, 1, _
False, "", False, "", "", "" "
' Extracts and displays the estimated number of search results.
Dim estresults as Integer = R.estimatedtotalresultscount
Lblsearchresults.text = CStr (estresults)
List 1:btnsearch_click method

Get the size of a Web page
As shown in Listing 2, it is easy to get the size of a cached Web page on Google server. This invokes the doGetCachedPage method, assigns it to a byte array, and then provides its length by the array.

' Create a Google Search object
Dim s as New Google.googlesearchservice ()
' Call the doGetCachedPage method and get the cached byte
Dim bytes () as System.Byte = _
S.dogetcachedpage (Txtlicensekey.text, Txtcachepage.text)
' Show the length of the cached page
Lblcacheresults.text = CStr (bytes. Length)
List 2:btncache_click method

Perform a spell check
The last Google method calls the Google engine's spell-checking feature. The Dospellingsuggestion method on the Googlesearchservice object is invoked, and if the result is obtained, we simply display the results.

' Create a Google Search object
Dim s as New Google.googlesearchservice ()
' Ask for spelling suggestions
Dim Suggestion as String = _
S.dospellingsuggestion (Txtlicensekey.text, Txtspell.text)
' Show suggestions (if any)
If suggestion is nothing Then
Lblspellresults.text = "< no recommendations >"
Else
Lblspellresults.text = Suggestion
End If
List 3:btnspell_click method

A note on exception handling
The downloaded code contains all the features in a Try ... Catch Block (see Listing 4). Use this block to catch any errors returned from the SOAP Web service and display them in a message box. The most common errors usually occur when you forget to enter a license key. To obtain a license key, see how to obtain a kit and license key.

' Create a Google Search object
Dim s as New Google.googlesearchservice ()
Try
' Call the doGetCachedPage method and get the cached byte
Dim bytes () as System.Byte = _
S.dogetcachedpage (Txtlicensekey.text, Txtcachepage.text)
' Show the length of the cached page
Lblcacheresults.text = CStr (bytes. Length)
Catch ex as System.Web.Services.Protocols.SoapException
MsgBox (ex. Message)
End Try
Listing 4: Btncache_click methods that contain exception handling

Summary
Visual Basic. NET provides a simple way to access Web services on the Web. Adding this functionality to your "brains" gives you a new feature-just as adding COM objects and ActiveX controls can add new functionality to your Visual Basic 6 applications. You can also try to add Google's spell checker to a web-based message board, or try to automatically monitor the web for up-to-date information on specific issues. For more inspiration, you can also search the web (try searching for "VB Google API Applications") to find applications developed by other VISUAL BASIC developers using the Google Web API. Hope everybody is happy!

How to obtain a toolkit and license key
To access the Google WEB API Toolkit and request a license key, visit http://www.google.com/apis/and follow its instructions. You can download the Developer toolkit and create your own Google account, which, after confirmation by email, will provide you with a license to make 1,000 free inquiries a day.


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.