Click here to Skip to main content
15,884,298 members
Articles / Programming Languages / C#
Article

Google Web Service Client program

Rate me:
Please Sign up or sign in to vote.
3.67/5 (12 votes)
27 Apr 2002 158.6K   864   57   24
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.

C#
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);
}

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Software Developer (Senior)
Korea (Republic of) Korea (Republic of)
Woo Seok Seo have been a Microsoft MVP for 7 years and have translated several books into Korean. Author of C# Programming for Beginner (DevPress, 2001), he is interested in Debugging techniques and .NET technology. Get in touch with Woo Seok Seo at wooseok.seo@gmail.com

Comments and Discussions

 
GeneralSerial key Pin
Bill_IT5-Apr-09 16:39
Bill_IT5-Apr-09 16:39 
GeneralRe: Serial key Pin
Umass3-Jun-09 6:18
Umass3-Jun-09 6:18 
GeneralRe: Serial key Pin
swdev.bali23-Jun-09 18:38
swdev.bali23-Jun-09 18:38 
Generalgoogle api Pin
Machvi31-May-08 10:42
Machvi31-May-08 10:42 
Generalthanks very much Pin
babylon12844-Apr-07 6:40
babylon12844-Apr-07 6:40 
QuestionLicense key Pin
BHAVANA RAO27-Mar-07 23:25
BHAVANA RAO27-Mar-07 23:25 
AnswerRe: License key Pin
Wooseok Seo28-Mar-07 0:27
Wooseok Seo28-Mar-07 0:27 
GeneralRe: License key Pin
nip9028-Mar-07 21:48
nip9028-Mar-07 21:48 
GeneralRe: License key Pin
Umass3-Jun-09 6:17
Umass3-Jun-09 6:17 
GeneralRe: License key Pin
nip903-Jun-09 7:09
nip903-Jun-09 7:09 
QuestionException Pin
sir Beans8-Sep-06 1:26
sir Beans8-Sep-06 1:26 
QuestionDataBinder.Eval() Pin
TheEagle29-Jun-06 22:14
TheEagle29-Jun-06 22:14 
GeneralNeed Help Pin
ovia28-Sep-05 0:56
ovia28-Sep-05 0:56 
GeneralHelp..... Pin
Wiele Met 'n P23-Sep-03 2:02
Wiele Met 'n P23-Sep-03 2:02 
Generalthanks Pin
ayman2-Jun-03 14:52
ayman2-Jun-03 14:52 
GeneralRe: thanks Pin
Mahmoud Nasr Ahmed3-Jun-03 22:55
Mahmoud Nasr Ahmed3-Jun-03 22:55 
GeneralNiiice Pin
Okeno Palmer5-Sep-02 16:33
Okeno Palmer5-Sep-02 16:33 
Dude, you beat me to it Smile | :) .. I just downloaded the API and was writing the app. I'll post my version when done still so y'all can see. This will be my first venture into the .NET arena too Smile | :)

..:: Keno ::..
GeneralDemo Froze up everytime Pin
TigerNinja_20-Aug-02 9:03
TigerNinja_20-Aug-02 9:03 
GeneralGood Job Pin
sacoskun16-Aug-02 3:34
sacoskun16-Aug-02 3:34 
General[Smile] Cool job! Pin
Wooseok Seo28-Apr-02 19:33
Wooseok Seo28-Apr-02 19:33 
GeneralRe: [Smile] Cool job! Pin
Nish Nishant28-Apr-02 19:41
sitebuilderNish Nishant28-Apr-02 19:41 
GeneralRe: [Smile] Cool job! Pin
asuleman18-Apr-03 22:12
asuleman18-Apr-03 22:12 
GeneralRe: [Smile] Cool job! Pin
Wooseok Seo29-Jun-03 4:44
Wooseok Seo29-Jun-03 4:44 
GeneralRe: [Smile] Cool job! Pin
guy kolb18-Apr-06 3:29
guy kolb18-Apr-06 3:29 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.