6. Web Services Lab

 

Overview:  you will create a WinForms client application that communicates with Google's web service to perform searches and display the results.  Note that the assignment requires an active connection to the internet. 


Programming against Google's Web Service

  1. This is a great example of the power of web services.  Google.com has a free web service (up to 1000 searches per day) that allows you to perform Google-based searches and manipulate / display the results any way you want.  In essence, you submit a query to Google, and you get back an array of result objects, each object containing a URL (among other things).  If you want to see the application in action before you code it, there is a solution provided in the solution\GoogleApp directory:  run the .EXE from the bin\Debug sub-directory. 

  2. Startup Visual Studio and create a new C# Windows application called "GoogleApp".  On the form place a text box for the search string, a label for displaying the estimated number of matches, a list box for displaying the resulting URLs, and a button for performing the search.  Make sure the list box is pretty wide since it will be used for displaying URLs.

  3. Add a web reference to your application to the Google web service.  Right-click on your project references, select web reference, and in the dialog box that appears, type the following URL to gain access to the necessary web service description file (WSDL):  http://api.google.com/GoogleSearch.wsdl.  Once added, the web service classes will exist within the namespace "com.google.api"; this should be the name you see in the list of Web References for your project.

  4. Program the search button to declare two local reference variables, one of type GoogleSearchResult and another of type GoogleSearchService.  Remember, these are declared within the namespace com.google.api, so you must either use their fully-qualified name, or import the namespace GoogleApp.com.google.api with a "using" statement.  Next, create an instance of GoogleSearchService.  Finally, call the doGoogleSearch() method using this instance and assign the result to your local GoogleSearchResult variable.  For now, code the first parameter as "", the second parameter is the search string (which you should take from the contents of the text box), the third parameter is 0 and the fourth is 10 (these ask Google to return the first 10 hits, numbered 0 .. 9, where 10 is the maximum number of matches you can request at once).  For the remaining parameters, pass "" for string and false for boolean.  Don't try to run yet, there's the issue of a license key...

  5. The first parameter of the doGoogleSearch() method must be a non-empty string containing a valid license key.  For the purposes of this lab, feel free to use my own personal license key: 

     

    4a8/TvZQFHID0WIWnL1CMmMx0sNqhG8H

     

    However, in the future, if you plan to work with Google's web service, please register for a free Google account and obtain your own personal license key.  You can register for an account at:  https://www.google.com/accounts/CreateAccount.

  6. After the call to doGoogleSearch(), an object is returned of type GoogleSearchResult.  This object contains a field called estimatedTotalResultsCount, which denotes the estimated number of hits based on your search string.  Convert this value to a string and display it in your label.  Finally, display a message box telling the user that the search is complete.  Now run!

  7. Once that works, the next step is to display the first 10 hits in your list box.  The GoogleSearchResult object contains a field called resultElements, which is an array of objects of type ResultElement.  Each ResultElement object contains a URL.  Loop through the resultElements array, displaying each URL in the list box.  Now run!  Wow!

  8. If you want to display more URLs, you need to make additional calls to Google via the doGoogleSearch() method.  All you need to do is change the 3rd parameter each time:  pass 10 to get matches 10 .. 20, then pass 20 to get matches 20 .. 30, and so on.  Write a simple loop to get the first 100 matches, and display these in the list box.  Run, and be amazed :-)

  9. How did I learn about all this?  Surf to http://www.google.com/apis/ for more information, API documentation, sample .NET code, etc.  Enjoy!


If time permits...

  1. Add a web browser control to your form, and if the user clicks on an item in the list box, navigate to this URL...  First you'll need to add a web browser control to your toolbox:  right-click on the toolbox, select customize, click on the COM tab, and scroll down to find "Microsoft Web Browser".  Click the checkbox to select this control, and then press OK.  Scroll to the bottom of your toolbox and you should see an Explorer icon.  Drag-and-drop one on your form.  Now program the form so that when the list box's SelectedIndexChanged event occurs, you call the web browser control's Navigate() method (the browser control'ss name should be axWebBrowser1).  The first parameter is the URL, the remaining 4 parameters are optional:  declare 4 local variables of type object, set them to null, and then pass them by-reference to the Navigate() method.  Run, and be even more amazed :-)

  2. That's it!