Using the Google Web Service

In Chapter 1, you took advantage of web services that exist at sites around the Web. The Google website provides a web service that you can use to perform queries within your programs, much like the queries you perform at the Google website. To begin, you must download the Google software development kit (SDK) from the Google website at http://www.google.com/apis/, as shown in Figure 3.10. Then, you must download a license from Google that you must use each time you call a Google web service method. The license key lets you make up to 1,000 calls to Google web service methods per day.

click to expand
Figure 3.10: Downloading the Google software development kit

To start, the following Visual Basic .NET program, SpellDemo.vb, uses the Google web service doSpellingSuggestion method to spell check one or more terms. If the words are spelled incorrectly, the method will return a suggested result. When you run the program, your screen will display a form that prompts the user for the text to be checked. After the user types the text and clicks the Verify Spelling button, the program will display the web service’s suggested alternatives, as shown in Figure 3.11.


Figure 3.11: Using the Google web service to spell check terms

To create the SpellDemo.vb program, perform these steps:

  1. Within Visual Studio .NET, select the File menu New Project option. Visual Studio .NET will display the New Project dialog box.

  2. Within the New Project dialog box Project Types list, click Visual Basic Projects. Then, within the Templates field, click ASP.NET Web Application. Finally, within the Location field, specify the folder within which you want to store the program and the program name SpellDemo. Select OK. Visual Studio .NET will display a page onto which you can drag and drop the program’s controls (label, buttons, and text box).

  3. Using the Toolbox, drag and drop the button and text boxes previously shown in Figure 3.11 onto the page.

  4. Select the Project menu Add Web Reference option. Visual Studio .NET will display the Add Web Reference dialog box.

  5. Within the Address field, you must type the URL of a special file (called the WSDL file) that describes the web service. In this case, type http://api.google.com/GoogleSearch.wsdl and press Enter. The dialog box will load the file’s contents. Click the Add Reference button.

  6. Select the View menu Code option. Visual Studio .NET will display the program’s source code. Within the source code add the following program statements:

        Private Sub Button1_Click(ByVal sender As System.Object, Ä   ByVal e As System.EventArgs) Handles Button1.Click       Ä     Dim WS As New com.google.api.GoogleSearchService()         Dim MyKey As String = "XXXXXxxxxXXXXXxxxxxXXXX+kB+USD"         If (TextBox1.Text.Length > 0) Then             Try                 TextBox2.Text = WS.doSpellingSuggestion(MyKey, Ä               TextBox1.Text)             Catch Ex As Exception                 TextBox2.Text = Ex.Message             End Try         End If     End Sub End Class

Within the Visual Basic source code, you must replace the X’s shown with your own Google key. The following section will illustrate how your programs can use the Google web service to search for sites on the Web—in other words, how you can integrate the Google search engine into the applications you write.

Searching for Text Using the Google Search Engine

The following program, GoogleSearchDemo.vb, displays a form that prompts the user to enter a search query. After the user enters the text and clicks the Search button, the program uses the Google web service to search for websites that contain the text. The program then displays a count of the number of links the service found and the first ten links, as shown in Figure 3.12.


Figure 3.12: Using the Google search engine from within a Visual Basic .NET program

To create the GoogleSearchDemo.vb program, perform these steps:

  1. Within Visual Studio .NET, select the File menu New Project option. Visual Studio .NET will display the New Project dialog box.

  2. Within the New Project dialog box Project Types list, click Visual Basic Projects. Then, within the Templates field, click Windows Application. Finally, within the Location field, specify the folder within which you want to store the program, and the program name, GoogleSearchDemo. Select OK. Visual Studio .NET will display a page onto which you can drag and drop the program’s controls (label, buttons, and text box).

  3. Using the Toolbox, drag and drop the button, labels, and text boxes previously shown in Figure 3.12 onto the page.

  4. Select the Project menu Add Web Reference option. Visual Studio .NET will display the Add Web Reference dialog box.

  5. Within the Address field, you must type the URL of a special file (called the WSDL file) that describes the web service. In this case, type http://api.google.com/GoogleSearch.wsdl and press Enter. The dialog box will load the file’s contents. Click the Add Reference button.

  6. Select the View menu Code option. Visual Studio .NET will display the program’s source code. Within the source code add the following program statements:

    Private Sub Button1_Click(ByVal sender As System.Object, ÄByVal e As System.EventArgs) Handles Button1.Click    Dim WS As New com.google.api.GoogleSearchService()    Dim MyKey As String = "XXXXXxxxxXXXXXxxxxxXXXX+kB+USD"    Dim Result As com.google.api.GoogleSearchResult    Dim Count = 0    If (TextBox1.Text.Length > 0) Then      Try        Result = WS.doGoogleSearch(MyKey, TextBox1.Text, 0, Ä               10, False, "", False, "", "", "")        TextBox2.Text = Result.estimatedTotalResultsCount        While (Count < 10 And Count < Result.estimatedTotalResultsCount)          TextBox3.Text = TextBox3.Text & Ä         Result.resultElements(Count).URL & vbCrLf & vbCrLf            Ä      Count = Count + 1        End While      Catch Ex As Exception        TextBox3.Text = Ex.Message      End Try    End If End Sub

Again, within the code, you must replace the X’s shown with your Google key. The following section illustrates how you can employ the web service behavior to use the Google web service from within an HTML file.

Calling the Google Web Service Using the Webservice Behavior

As you have learned, to use the Webservice behavior to call a web service method, the web service must reside on the same web domain as the HTML file that is calling the service. Otherwise, you must create a web service that serves as a proxy to the remote service.

Because the Google web service resides on a domain other than that which contains your HTML file, you must create a proxy service that the Webservice behavior can use to interact with the Google service. The following GoogleProxy web service provides the SpellCheck method that returns an alternative spelling for specified text:

string SpellCheck(string)

To create the GoogleProxy web service, perform these steps:

  1. Within Visual Studio .NET, select the File menu New Project option. Visual Studio .NET will display the New Project dialog box.

  2. Within the New Project dialog box Project Types list, click C# Projects. Then, within the Templates field, click ASP.NET Web Service. Within the Location fields, type GoogleProxy. Select OK. Visual Studio .NET will display a page onto which you can drag and drop the program’s components.

  3. Select the View menu Code option. Visual Studio .NET will display the service’s source code.

  4. Before a web service can call a second web service, you must add a Web Reference to the remote service. To do so, select the Project menu Add Web Reference option. Visual Studio .NET will display the Add Web Reference dialog box.

  5. Within the Address field, type the URL of the WSDL file that describes the web service. In this case, type http://api.google.com/GoogleSearch.wsdl and press Enter. The dialog box will load the file’s contents. Click the Add Reference button.

  6. Within the source code add the following program statements:

    <WebMethod()> Public Function SpellCheck(ByVal Text As String) Ä As String     Dim WS As New com.google.api.GoogleSearchService()     Dim MyKey As String = "Ovl88fNQFHL1FR1WkQVl1tRJw+kB+USD"     Try       SpellCheck = WS.doSpellingSuggestion(MyKey, Text)     Catch Ex As Exception       Throw Ex     End Try End Function

Next, the following HTML file, GoogleSpellCheck.html, displays a form within which a user can type text that he or she wants to spell check. After the user types the text and clicks the Spell Check button, the program will display an alternative spelling for the text as shown in Figure 3.13. Listing 3.9 implements the GoogleSpellCheck.html file.

click to expand
Figure 3.13: Using the Google web service from within an HTML file

Listing 3.9 GoogleSpellCheck.html

start example
<html> <head>  <title>GoogleSpellCheck</title>  <script language="JavaScript">  function InitializeService()   {  service.useService("http://localhost//GoogleProxy/Service1.asmx?wsdl", Ä "ProxyService");   }   function CheckSpelling()   {    service.ProxyService.callService("SpellCheck", Ä   document.DemoForm.TargetText.value);   }   function ShowResult()   {     document.DemoForm.Alternative.value = event.result.value;   }   </script> </head> <body onload="InitializeService()"        style="behavior:url(webservice.htc)"       onresult="ShowResult()"> <form name="DemoForm">    Text to check:  <input name="TargetText" Columns="60"><br/>    Result: <input name="Alternative" Columns="60"><br/>    <button onclick="CheckSpelling()">Spell Check</button><br> </form> </body> </html>
end example

Again, within the HTML <body> tag, the file specifies the webservice.htc behavior and when the web service returns its result, the browser will call the ShowResult method. The remainder of the JavaScript code is quite similar to the other HTML files that this chapter has presented. The InitializeService method specifies the proxy service’s WSDL file and the service’s “friendly name.” The CheckSpelling function uses the callService method to call the service’s SpellCheck method, and the ShowResult method displays the web service’s result.




. NET Web Services Solutions
.NET Web Services Solutions
ISBN: 0782141722
EAN: 2147483647
Year: 2005
Pages: 161
Authors: Kris Jamsa

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net