Google Optimization
  Home arrow Google Optimization arrow Page 4 - Using the Google SOAP Search API
SEO Chat Forums  
Choosing Keywords  
Google Optimization  
Link Trading  
MSN Optimization  
Search Engine News  
Search Engine Spiders  
Search Optimization  
Web Directories  
Website Marketing  
Website Promotion  
Website Submission  
Yahoo Optimization  
SEO Tools
Adsense Calculator
AdSense Preview
Advanced Meta-Tags
Alexa Rank Tool
Check Server Headers
Class C Checker
Code to Text Ratio
CPM Calculator
Domain Age Check
Domain Typos
Future PageRank
Google Dance
Google Keywords
Google Search
Google Suggest
Google vs Yahoo
Indexed Pages
Keyword Cloud
Keyword Density
Keyword Difficulty
Keyword Optimizer
Keyword Position
Keyword Typos
Link Popularity
Link Price Calculator
Meta Analyzer
Meta Tag Generator
Multiple Link Popularity
Page Comparison
Page Size
PageRank Lookup
PageRank Search
Robots.txt Generator
ROI Calculator 
S.E. Comparison 
S.E. Keyword Position 
Site Link Analyzer 
Spider Simulator 
URL Redirect Check 
URL Rewriting 
Mobile Linux 
APP Generation ROI 
IBM® developerWorks 
SEO Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
GOOGLE OPTIMIZATION

Using the Google SOAP Search API
By: Peyton McCullough
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 20
    2006-09-27

    Table of Contents:
  • Using the Google SOAP Search API
  • Performing a Google Search
  • Cached Pages
  • Asynchronous Calls

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    Using the Google SOAP Search API - Asynchronous Calls


    (Page 4 of 4 )

    So far, all of our calls to the SOAP Search API service have been synchronous. However, the API also provides asynchronous functionality. Instead of waiting for a result to be returned before performing any more work, you can send a message to the service and do something else while you wait for a reply.

    The API actually provides two ways to do this. The first method involves subscribing to the appropriate event and then calling a special version of whichever “do-” method you wish to use. Here, we perform an asynchronous Google search using this approach:

    usingSystem;
    usingSystem.Text.RegularExpressions;
    classGoogleAsyncTest
    {
        private static Regex stripHtml = new Regex("<(.+?)>");
        static void Main()
        {
            GoogleSearchService google = new GoogleSearchService();
            google.doGoogleSearchCompleted += new
    doGoogleSearchCompletedEventHandler(OnSearchCompleted);
            google.doGoogleSearchAsync("x0x0", "Cookie Recipe", 0,
    10, true, "", true, "", "", "");
            Console.ReadKey();
        }
        static void OnSearchCompleted(object sender,
    doGoogleSearchCompletedEventArgs
    e)
        {
            Console.WriteLine("Total results: " +
    e.Result.estimatedTotalResultsCount);
            foreach (ResultElement result in e.Result.resultElements)
            {
                Console.WriteLine("n" + stripHtml.Replace
    (result.title, ""));
                Console.WriteLine(stripHtml.Replace(result.snippet,
    ""
    ));
            }
        }
    }

    Above, we subscribe the OnSearchCompleted static method to the onGoogleSearchCompleted event. We then call doGoogleSearchAsync and pass all the usual arguments. When the SOAP Search API service completes the search, the OnSearchCompleted method is called. Here, we display the estimated total number of results and then iterate through the ten results we receive. This information is contained within a doGoogleSearchCompletedEventArgs object.

    There is also an alternate version of the doGoogleSearchAsync method that accepts an additional argument, an object. For example, here we pass a string:

        static void Main()
        {
           ...
            string state = "This represents the current state.";
            google.doGoogleSearchAsync("x0x0", "Cookie Recipe", 0, 10, true, "", true, "", "", "",
                state);
            ...
        }

    The object is intended to represent the current state—anything we want to make use of later on in the OnSearchCompleted method. There, we can cast it to whatever type (here, a string):

        static void OnSearchCompleted(object sender,
    doGoogleSearchCompletedEventArgs e)
        {
            ...
            string state = (string)e.UserState;
            ...
        }

    In fact, the state object argument is mandatory when more than one asynchronous operation is in progress.

    Cached pages and spelling suggestions are handled in much the same way:

        static void Main()
        {
            google.doGetCachedPageCompleted +=
                new doGetCachedPageCompletedEventHandler
    (OnGetCachedPageCompleted);
            google.doSpellingSuggestionCompleted +=
                new doSpellingSuggestionCompletedEventHandler
    (OnSpellingSuggestionCompleted);
            ...
            google.doGetCachedPageAsync("x0x0", "http://google.com",
    "State 1"
    );
            google.doSpellingSuggestionAsync("x0x0", "gogles", "State
    2"
    );
            ...
        }
        static void OnGetCachedPageCompleted(object sender,
    doGetCachedPageCompletedEventArgs
    e)
        {
            byte[] page = e.Result;
            // Do whatever here
        }
        static void OnSpellingSuggestionCompleted(object sender,
            doSpellingSuggestionCompletedEventArgs e)
        {
            string spelling = e.Result;
            // Do whatever here
        }

    The second way to use the API asynchronously utilizes callbacks rather than events. For example, to conduct a search, the BegindoGoogleSearch method is called. It takes the usual arguments, in addition to an AsyncCallback object (which points to the method that will be called when the search is complete) and a state object. It returns an object in the form of theIAsyncResult interface:

    usingSystem;
    usingSystem.Text.RegularExpressions;
    classGoogleAsyncTest2
    {
        private static Regex stripHtml = new Regex("<(.+?)>");
        private static GoogleSearchService google = new
    GoogleSearchService();
        static void Main()
        {
            IAsyncResult ar = google.BegindoGoogleSearch("x0x0",
    "Cookie Recipe"
    , 0, 10, true, "",
                true, "", "", "", new AsyncCallback
    (OnSearchCompleted), "State");
                "State");
            Console.ReadKey();
        }
    (continued)
    ...

    In the OnSearchCompleted method, we can retrieve the state object, but before we can see the results of our search, we must call the EnddoGoogleSearch method, which returns a GoogleSearchResult object:

     ...
       static void OnSearchCompleted(IAsyncResult ar)
        {
            string state = (string) ar.AsyncState;
            GoogleSearchResult search = google.EnddoGoogleSearch(ar);
            Console.WriteLine("Total results: " +
    search.estimatedTotalResultsCount);
            foreach (ResultElement result in search.resultElements)
            {
                Console.WriteLine("n" + stripHtml.Replace
    (result.title, ""));
                Console.WriteLine(stripHtml.Replace(result.snippet,
    ""
    ));
            }
        }

    Again, the process for getting cached pages or spelling suggestions is nearly identical:

        static void Main()
        {
            ...
            IAsyncResult ar2 = google.BegindoGetCachedPage
    ("hW0StcxQFHJl8dGmsUxL/J3zS+fGcZs6",
                "http://msn.com", new AsyncCallback
    (OnGetCachedPageCompleted), "State 2");
            IAsyncResult ar3 = google.BegindoSpellingSuggestion
    ("hW0StcxQFHJl8dGmsUxL/J3zS+fGcZs6",
                "telephoen", new AsyncCallback
    (OnSpellingSuggestionCompleted), "State 3");
            ...
        }
        static void OnGetCachedPageCompleted(IAsyncResult ar)
        {
            byte[] page = google.EnddoGetCachedPage(ar);
            Console.WriteLine(page);
        }
        static void OnSpellingSuggestionCompleted(IAsyncResult ar)
        {
            string spelling = google.EnddoSpellingSuggestion(ar);
            Console.WriteLine(spelling);
        }

    Conclusion

    Though still in beta, the Google SOAP Search API appears to be very promising for developers who wish to implement a few of Google's features into their own applications. Currently, it offers support for Web searches, retrieval of cached pages and spelling suggestions. These three popular services can be easily accessed through the API, either synchronously or asynchronously, to create more complex and informed applications with .NET.


    DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware.

       · The article was very informative. I liked it very much as i have got some...
       · Thank you for your comment!The Google SOAP Search API is pretty interesting, but...
       · Google seems to implictly (but not explicitly) accept everyone using their Google...
     

    GOOGLE OPTIMIZATION ARTICLES

    - Google Adwords Guide
    - Create a Customized Google XML Sitemap
    - Google Website Optimizer Review
    - Using Analytics to Understand Your Readers
    - Google Sandbox Effect
    - Improve Google Indexing on Your Ecommerce We...
    - Back Links and Google Page Rank
    - The Five Most Important Things You Can Do wi...
    - Advanced Traffic Analysis Techniques with Go...
    - How to Build an Online Survey with Google Do...
    - How to Analyze and Rate Keyword Difficulty i...
    - Using Analytics on Your Site
    - Google Algorithms
    - How Google`s Quality Raters Treat Web Spam
    - Google`s Quality Rater Guidelines Leaked



     



    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 4 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek