在 C# WinForms 应用程序中使用 Yahoo! Local Search Service
一篇关于在 C# WinForms 应用程序中使用 Yahoo! Local Search Service 的文章。

引言
Yahoo Web 服务通过 Yahoo 的开发者网络向开发者提供;提供了多种服务,但本文特别关注使用 Yahoo 的本地搜索服务。此服务将根据基于邮政编码的业务名称或产品搜索,返回一个企业列表。 该服务返回以下信息
- 标题(企业的名称)
- Street Address
- 城市
- 状态
- 电话号码
- 纬度和经度
- 与用户提供的邮政编码中心的距离
- 有关企业的地图和详细信息的 URL
- 企业网站的 URL(如果可用)
本文将演示提交本地搜索服务请求、显示结果以及访问在搜索结果中返回的可用链接的基础知识。 当然,我们可以用多种方式呈现搜索结果,而这个演示应用程序仅说明了其中的一种可能性。 有关该程序的更多信息,请直接参考 Yahoo 开发者网络网站,网址为 这里。
该服务每天最多可访问 50,000 次; 参与该计划是免费的,但前提是注册 Yahoo! 开发者网络并从 Yahoo! 获得应用程序 ID
访问 Yahoo! 网站时,请查看提供的不同程序,并查看通过 Yahoo 提供的许多服务中的一些服务。





入门
为了开始,请解压缩包含的项目并在 Visual Studio 2005 环境中打开解决方案。 在解决方案资源管理器中,您应该注意这些文件

主窗体 (frmSearch.cs)
主窗体 (frmSearch.cs) 是应用程序中包含的唯一窗体; 访问服务、返回本地搜索结果以及将这些结果显示在 datagridview 控件或 Web 页面中所需的所有应用程序特定代码都包含在此类中。
代码非常简单,如果您想在 IDE 中打开代码视图,您将看到代码文件开头如下
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Net;
导入主要基于 Windows 应用程序的默认配置; `System.Net` 库导入是与默认值的唯一偏差。
在导入之后,定义命名空间和类,并添加一个默认构造函数。 请注意,在构造函数中; 邮政编码文本框使用存储的用户设置填充。 此代码和所有剩余代码都标有注释,用于描述每个代码部分的目的。
namespace YahooLocalSearch
{
/// <summary>
/// Consume the Yahoo! Local Search Service
/// in a C# WinForms application
/// </summary>
public partial class frmSearch : Form
{
/// <summary>
/// default constructor
/// </summary>
public frmSearch()
{
InitializeComponent();
// the user's zip code likely won't change so
// set the zip code to user last entered zip code
txtZipCode.Text = Properties.Settings.Default.MyZipCode;
}
代码的下一部分是搜索按钮的单击事件处理程序。 单击此按钮时,该方法将验证用户是否输入了搜索字词和邮政编码。 它会将当前的邮政编码存储到用户设置中(以防用户更改它),然后它将调用一个名为 `LocalSearch` 的单独方法,该方法接受两个参数:企业名称或产品名称,以及邮政编码。
/// <summary>
/// Search for matches based upon user supplied
/// business/product name and zip code
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnSearch_Click(object sender, EventArgs e)
{
// validate that search terms were entered
// by making sure that the business/product
// and the zip code textboxes contain something
if (txtBusinessOrProduct.Text != string.Empty ||
txtZipCode.Text != string.Empty)
{
// save last entered zip code in user setting
// so that the next time the application is used
// we can retrieve this zip code and reuse it
Properties.Settings.Default.MyZipCode = txtZipCode.Text;
Properties.Settings.Default.Save();
// call the search with the product and zip code arguments
// this function will populate the datagrid with the
// values returned from the product search
LocalSearch(txtBusinessOrProduct.Text, txtZipCode.Text);
}
else
{
// tell the user that the search requested is invalid
// if they failed to fill in both the product/business and
// zip code textboxes
MessageBox.Show("You must supply both a zip code and product
or business name.", "Search Invalid");
}
}
代码的下一部分用于执行本地搜索。 代码的第一部分将企业或产品名称存储到字符串变量中,然后修剪邮政编码字符串以防止邮政编码 + 4 的条目; 接受邮政编码的文本框的长度限制为仅五个字符,因此这实际上是不必要的。 然后格式化一个 HTTP Web 请求,以针对 Yahoo! 本地搜索服务执行搜索; 此请求使用提供的企业或产品名称以及提供的邮政编码来限制返回的结果数量。 该服务将结果限制为最多 20 个条目。
派生的响应流用于填充数据集; 然后将数据集的第二个表绑定到 datagridview。 第二个表包含搜索结果; 该服务返回总共三个表,其中包含有关企业评级和搜索返回的总点击次数的信息。
/// <summary>
/// Conduct a search for products or business names
/// within a specified zip code using the Yahoo!
/// Local Search Service
/// </summary>
/// <param name="business"></param>
/// <param name="zipCode"></param>
private void LocalSearch(string business, string zipCode)
{
try
{
// business or product name
string biz = business;
// take only the zip code (not zip + 4)
string zip = zipCode.Substring(0, 5);
// format the web request using
// the zip code and business/product name
// passed in as arguments
// NOTE: The appid should be replaced with the
// appid supplied by Yahoo! when the developer
// signs up with the free developer network
System.Net.HttpWebRequest request
= System.Net.WebRequest.Create
("http://local.yahooapis.com/LocalSearchService/V2/localSearch?
appid=YahooDemo&query=" + biz + "&zip=" + zip + "&results=20") as
System.Net.HttpWebRequest;
// get http web response for the web request
using (System.Net.HttpWebResponse response =
request.GetResponse() as System.Net.HttpWebResponse)
{
// populate the dataset using the
// response stream (in XML)
DataSet dsSearch = new DataSet();
dsSearch.ReadXml(response.GetResponseStream());
// populate the datagridview control using the
// second table (which contains the actual search
// results - in all three tables are returned
dataGridView1.DataSource = dsSearch.Tables[1];
dataGridView1.Refresh();
}
}
catch (Exception ex)
{
// describe any encountered error if the
// local search should fail for any
// reason
MessageBox.Show(ex.Message, "Error");
}
}
该类中包含的最后三个方法用于打开业务、地图和详细信息链接,这些链接在任何特定业务的结果中提供。
/// <summary>
/// Open up a browser window navigating to the
/// Map URL supplied with the search results
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void mapURLToolStripMenuItem_Click(object sender,
EventArgs e)
{
try
{
// get the cell value for the map URL
string url =
dataGridView1.SelectedRows[0].Cells[10].Value.ToString();
// open the URL into a browser window
System.Diagnostics.Process.Start(url);
}
catch
{
// display an error message to the user
// if the URL does not exist or was invalid
MessageBox.Show("Invalid URL", "URL Error");
}
}
/// <summary>
/// Open up a browser window navigating to the business
/// details URL returned with the search results
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void detailsToolStripMenuItem_Click(object sender,
EventArgs e)
{
try
{
// get the cell value for the details URL
string url =
dataGridView1.SelectedRows[0].Cells[8].Value.ToString();
// open the URL into a browser window
System.Diagnostics.Process.Start(url);
}
catch
{
// display an error message to the user
// if the URL does not exist or was invalid
MessageBox.Show("Invalid URL", "URL Error");
}
}
/// <summary>
/// Open up a browser window navigating to the business
/// owned website (if available).
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void businessURLToolStripMenuItem_Click(object sender,
EventArgs e)
{
try
{
// get the cell value for the business URL
string url =
dataGridView1.SelectedRows[0].Cells[11].Value.ToString();
// open the URL into a browser window
System.Diagnostics.Process.Start(url);
}
catch
{
// display an error message to the user
// if the URL does not exist or was invalid
MessageBox.Show("Invalid URL", "URL Error");
}
}
摘要
此应用程序被提供作为如何在 C# WinForms 应用程序中利用 Yahoo! 本地搜索服务的一个示例。 该服务返回有关企业的信息。 它还返回一个链接集合,这些链接可用于显示企业的地图、企业网站和 Yahoo! 收集的与企业相关的信