65.9K
CodeProject is changing. Read more.
Home

Google Web Service Client program

starIconstarIconstarIcon
emptyStarIcon
starIcon
emptyStarIcon

3.67/5 (12 votes)

Apr 28, 2002

viewsIcon

159480

downloadIcon

864

A very simple client program that uses Google's web service

Sample Image

This is very simple client program that uses Google's web service. You can download the SDK from Google.

The sample code is like the following.

private void buttonSearch_Click(object sender, System.EventArgs e)
{
    // before search

    //

    labelSearchText.Text = "Searching...";
    labelSearchText.Update();


    // create Google Search object

    //

    GoogleSearchService s = new GoogleSearchService();
    GoogleSearchResult r;


    // call search function

    //

    r = s.doGoogleSearch(
        "",  ; You license key!
        textSearch.Text, 
        0, 
        10, 
        false, "", false, "", "", "");


    // create HTML document to show result

    //

    string strFile = "result.html";
    StreamWriter sw = File.CreateText(strFile);


    // Header inforamtion

    //

    sw.WriteLine("<HTML><HEAD></HEAD><BODY>");


    // Category 

    //

    foreach(DirectoryCategory dc in r.directoryCategories)
    {
        sw.Write("<b>Category</b> : ");
        sw.WriteLine(dc.fullViewableName);
        sw.WriteLine("<br><br><br>");
    }


    // iterate items

    //

    foreach(ResultElement re in r.resultElements)
    {
        // Title

        //

        string strTitle = "<a href=\"" + re.URL + "\">" + 
                          re.title + "</a><br>";
        sw.WriteLine(strTitle);
        
        // snippet

        //

        string strSnippet = re.snippet +"<br>";
        sw.WriteLine(strSnippet);

        // link and cache size

        //

        string strLink = "<a href=\"" + re.URL + "\">" + re.URL + "</a> - " 
                         + re.cachedSize + "<br><br>";
        sw.WriteLine(strLink);

        // 2 line

        //

        sw.WriteLine("<br><br>");
    }


    // file close

    //

    sw.Close();

    
    // result inforamtion

    //

    labelSearchText.Text = textSearch.Text + " 's web search";

    int estResults = r.estimatedTotalResultsCount;
    double ldTime = r.searchTime;
    labelSearchResult.Text = "Total " + Convert.ToString(estResults) + "  " + 
                    "1 - 10 seach result  Total time:" + 
                                         Convert.ToString(ldTime);


    // browsing!

    //

    object obj = null;
    DirectoryInfo di = new DirectoryInfo(Environment.CurrentDirectory);
    string strFilePath = di.FullName + "\\" + strFile;
    WebBrowser.Navigate(strFilePath, ref obj, ref obj, ref obj, ref obj);
}


// Google API homepage
//
private void linkLabel1_LinkClicked(object sender, 
                      System.Windows.Forms.LinkLabelLinkClickedEventArgs e)
{
    object obj = null;

    WebBrowser.Navigate("http://www.google.com/apis/", ref obj, ref obj, 

                        ref obj, ref obj);
}