Google Web Service 客户端程序






3.67/5 (12投票s)
2002年4月28日

159979

892
一个非常简单的使用 Google 网站服务的客户端程序
这是一个非常简单的客户端程序,它使用了Google的网络服务。您可以从Google下载SDK。
示例代码如下所示。
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);
}