[Windows Mobile]取得 Bing 與 Google 搜尋的結果
1. 簡介
Bing 與 Google 是常用的搜尋引擎,而 Bing 與 Google 分別有 Bing API 與 Google Web API 可以讓程式獲取搜尋結果,
本文實做行動裝置應用程式,功能為取得 Bing 與 Google 搜尋的結果。
2. 方法
2.1 使用 Bing API
(1) 申請 AppID
網址 http://www.bing.com/developers/default.aspx,使用 Live Passport 登入,並輸入相關資訊,註冊後得到一組 AppID
(2) 加入 Web 參考
在 [URL] 中輸入 http://api.search.live.net/search.wsdl?AppID=申請的AppID,修改 [Web 參考名稱],點選 [加入參考]
在程式中 using 專案名稱.參考名稱
using SmartDeviceSearch.net.live.search.api;
(3) 程式碼
#region Bing API
private void btnBing_Click(object sender, EventArgs e)
{
// LiveSearchService implements IDisposable.
using (LiveSearchService service = new LiveSearchService())
{
try
{
SearchRequest request = BuildRequestWeb();
// Send the request; display the response.
SearchResponse response = service.Search(request);
DisplayResponseWeb(response);
}
catch (System.Net.WebException ex)
{
// An exception occurred while accessing the network.
Console.WriteLine(ex.Message);
}
}
}
private SearchRequest BuildRequestWeb()
{
SearchRequest request = new SearchRequest();
// Common request fields (required)
request.AppId = "B692A148D1624C4E3C1248C8E5DDC209E524D2C4";
request.Query = this.txtQuery.Text;
request.Sources = new SourceType[] { SourceType.Web };
// Common request fields (optional)
request.Version = "2.0";
request.Market = "zh-tw";
request.Adult = AdultOption.Moderate;
request.AdultSpecified = true;
request.Options = new SearchOption[]
{
SearchOption.EnableHighlighting
};
// Web-specific request fields (optional)
request.Web = new WebRequest();
request.Web.Count = 10;
request.Web.CountSpecified = true;
request.Web.Offset = 0;
request.Web.OffsetSpecified = true;
request.Web.Options = new WebSearchOption[]
{
WebSearchOption.DisableHostCollapsing,
WebSearchOption.DisableQueryAlterations
};
return request;
}
private void DisplayResponseWeb(SearchResponse response)
{
// Display the results header.
string strHeader = "";
strHeader += "Bing API" + System.Environment.NewLine;
strHeader += "Results Count : " + response.Web.Total;
this.labBing.Text = strHeader;
DataTable Table_Results = new DataTable();
Table_Results.Columns.Add(new DataColumn("Title", typeof(string)));
Table_Results.Columns.Add(new DataColumn("Description", typeof(string)));
Table_Results.Columns.Add(new DataColumn("link", typeof(string)));
// Display the Web results.
foreach (WebResult result in response.Web.Results)
{
DataRow row = Table_Results.NewRow();
row[0] = result.Title;
row[1] = result.Description;
row[2] = result.Url;
Table_Results.Rows.Add(row);
}
this.dataGridBing.DataSource = Table_Results;
}
#endregion
2.2 使用 Google API
(1) 加入 Web 參考
在 [URL] 中輸入 http://api.google.com/GoogleSearch.wsdl,修改 [Web 參考名稱],點選 [加入參考]
在程式中 using 專案名稱.參考名稱
using SmartDeviceSearch.com.google.api;
(2) 程式碼
#region Google API
private void btnGoogle_Click(object sender, EventArgs e)
{
// GoogleSearchService implements IDisposable.
GoogleSearchService service_google = new GoogleSearchService();
GoogleSearchResult result = service_google.doGoogleSearch("a5/RXzlQFHKb8PhlejS5MC3nRkK6ngot", this.txtQuery.Text, 0, 10, false, "", true, "lang_zh-TW", "", "");
// Display the results header.
string strHeader = "";
strHeader += "Google API" + System.Environment.NewLine;
strHeader += "Results Count : " + result.estimatedTotalResultsCount;
this.labGoogle.Text = strHeader;
// Display the Web results.
DataTable Table_Results = new DataTable();
Table_Results.Columns.Add(new DataColumn("Title", typeof(string)));
Table_Results.Columns.Add(new DataColumn("Description", typeof(string)));
Table_Results.Columns.Add(new DataColumn("link", typeof(string)));
for (int i = 0; i < result.resultElements.Length; i++)
{
DataRow row = Table_Results.NewRow();
row[0] = result.resultElements[i].title;
row[1] = result.resultElements[i].snippet;
row[2] = result.resultElements[i].URL;
Table_Results.Rows.Add(row);
}
this.dataGridGoogle.DataSource = Table_Results;
}
#endregion
3. 結果
輸入要搜尋的關鍵字,點選 [Google Search] 按鈕,顯示搜尋結果於左邊 DataGrid
*註 : 使用Google API 一次能最多能取得10筆結果
點選 [Bing Search] 按鈕,顯示搜尋結果於右邊 DataGrid
4. 參考
How to Integrate Google Searches into Your Application
Bing API in Action: Search with C#
5. 附錄 : 檔案下載