Mining Google Web Services: Building Applications with the Google API

Google caches Web pages as a means to obtain the data when the original source changes or becomes unavailable. You can use a cached version of a page for comparison purposes or when the site goes offline for some reason. I've often used cached pages to retrieve data that the original Web site no longer carries. The actual uses for cached pages are unlimited.

The problem with cached pages is that they contain old data. You can't depend on a cached page to provide current information you need to make a decision on a research requirement or business need. Consequently, you need to define when a cached page is acceptable or even required as part of your application setup. The user needs guidelines on how to best use this feature with your particular application. With this in mind, Listing 10.2 shows how to obtain cached data from Google Web Services and display it on screen. You'll find this example in the \Chapter 10\CachedPage folder of the source code located on the Sybex Web site.

Listing 10.2: Obtaining Cached Data

private void btnTest_Click(object sender, System.EventArgs e) { GoogleSearchService GSS; // Interacts with Google. Byte[] Response; // The cached page. Char[] CResp; // A char array of the page. Int32 Counter; // Loop counter. String Results; // The page as a string. StreamWriter DataOut; // Disk storage for data. Object Blank; // A blank object. // Initialize the service. GSS = new GoogleSearchService(); // Get the cached page. Response = GSS.doGetCachedPage(@txtLicense.Text, @txtCached.Text); // Convert the cached data to a character array. CResp = new Char[Response.Length]; for (Counter = 0; Counter < Response.Length; Counter++) CResp[Counter] = Convert.ToChar(Response[Counter]); // Use the character array to create a string. Results = new String(CResp); // Save the string to a file. DataOut = new StreamWriter(@txtOutput.Text); DataOut.Write(Results); DataOut.Close(); // Display the cached data on screen. Blank = new object(); wbOutput.Navigate(@txtOutput.Text, ref Blank, ref Blank, ref Blank, ref Blank); }

 

The code begins by creating the GoogleSearchService object, GSS , as usual. In this case, the request arguments are relatively simple, so all you need to do is request them from the user and input them directly to the GSS.doGetCachedPage() method call.

Unfortunately, the data you receive isn't ready to use. Google sends the data as a base 64 encoded data stream. (See the "Defining the Cache Request Arguments" and "Defining the Cached Page Results" sections of Chapter 4 for details on this process.) The example overcomes this problem by converting the resulting Byte array to a Char array. The .NET Framework provides the means to use a Char array as input to the String() constructor.

At this point, you have a string that contains the HTML for the Web site. Viewing this string shows that it contains the proper escapes for saving the data to disk. You can't simply place the information on screen and hope that someone will read it. Consequently, the code creates a StreamWriter object and uses it to place the data on disk.

The wbOutput object is a standard COM Microsoft Web Browser control. You can add the Web browser to your toolbox by selecting the Microsoft Web Browser option shown in Figure 10.1 from the Customize Toolbox dialog box (accessed by right-clicking the Toolbox and selecting Add/Remove Items from the context menu).

Figure 10.1: You can use a standard Microsoft Web Browser control to display the results.

The IDE automatically builds the required interface code for you. However, the resulting wbOutput.Navigate() method requires entries for all of the arguments, even though you don't need to provide them to make the control work. The example gets around this problem by supplying a blank object as input that the control simply ignores. When the control navigates to the data saved on disk, it displays it as shown in Figure 10.2.

Figure 10.2: Cached pages are relatively easy to use once you convert the data.

Категории