Bing API初体验

简介:
作者:马宁

Bing正式发布没几天,除了功能和搜索结果外,作为开发者来说,我们关心的还有Bing API啥时候能出。周末浏览MSDN网站时,发现Bing Service已经上线了,链接是:http://msdn.microsoft.com/en-us/library/dd900818.aspx

Bing提供的API很丰富,除了搜索外,还增加了广告Ad、图片、新闻、Phonebook、拼写和视频的搜索。而访问协议有三种:JSON, XML和SOAP。JSON协议用于AJAX应用,XML用于Silverlight应用,SOAP用于传统的.NET等强类型程序。可见,微软在推出API方面还是很有效率的。

使用Bing API的第一步,是去Bing Developer Center上申请一个AppId,每个应用应该使用一个单独的AppId。Bing Developer Center的网址是:http://bing.com/developers 。在页面里先用Live ID登录,然后选择Get a new App ID,填写一些基本信息,然后你就会得到一串很长的AppId。需要注意的是,Bing还有一个网址是http://www.bing.com/developer/ ,估计是为1.1版本准备的,现在还不能申请AppId。大家一定要分清楚。

接下来,我们在Visual Studio 2008里创建一个.NET应用。在Project菜单里选择Add Service Reference.

注意:AppID=后要填写你申请到的AppId.

BingApi

在找到LiveSearchService的引用后,将其添加到我们的工程中。接下来,我根据PhoneBook和WebSearch两个例子写了DEMO,更多例子可以参考:

http://msdn.microsoft.com/en-us/library/dd251066.aspx

需要提醒的是,可能是文档没有更新,Bing API的类名称还会发生变化。我发现在2009年6月8日导出的引用中,LiveSearchService的名称变成了LiveSearchPortTypeClient。Web Search的代码如下:

private void button2_Click(object sender, EventArgs e)
        {
            // LiveSearchService implements IDisposable.
            using (LiveSearchPortTypeClient service = new LiveSearchPortTypeClient())
            {
                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 = AppId;
            request.Query = "马宁";
            request.Sources = new SourceType[] { SourceType.Web };

            // Common request fields (optional)
            request.Version = "2.0";
            request.Market = "en-us";
            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 = 30;
            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.
            listBox1.Items.Add("Bing API Version " + response.Version);
            listBox1.Items.Add("Web results for " + response.Query.SearchTerms);
            listBox1.Items.Add(string.Format("Displaying {0} to {1} of {2} results",
                response.Web.Offset + 1,
                response.Web.Offset + response.Web.Results.Length,
                response.Web.Total));

            // Display the Web results.
            System.Text.StringBuilder builder = new System.Text.StringBuilder();
            foreach (WebResult result in response.Web.Results)
            {
                builder.Length = 0;
                builder.AppendLine(result.Title);
                builder.AppendLine(result.Description);
                builder.AppendLine(result.Url);
                builder.Append("Last Crawled: ");
                builder.AppendLine(result.DateTime);

                listBox1.Items.Add(builder.ToString());
                Console.WriteLine();
            }
        }

从代码上来看,很简单,先创建一个LiveSearchPortTypeClient的对象,然后,创建SearchRequest对象,在Request里需要设置的是AppId,Query和Sources。AppId不用多说了,Query里填我们要查的关键字,Sources里指定SourceType,我们这里指定的是SourceType.Web。

image

将SearchRequest参数传递给LiveSearchPortTypeClient的Search方法,会返回一个SearchResponse的对象,里边包含我们的搜索结果。结果会包含在response.Web.Results对象里,最主要的参数是Title、Description和Url。

最后的运行结果就是这样的了:

BingApi2

Bing的好坏还需要时间检验,但是Bing API和Google API应该差不多,而且考虑了不同用户的需求,这也许就是软件公司和互联网公司不一样的地方。同时推出的还有Bing Map API,改天试一下。

 



本文转自马宁博客园博客,原文链接:http://www.cnblogs.com/aawolf/archive/2009/06/08/1498696.html,如需转载请自行联系原作者

目录
打赏
0
0
0
0
66
分享
相关文章
怎么申请 bing api key
1:打开网址 https://login.live.com/ 注册帐号并登录(点击上图中的登录按钮即可),在新窗口点击下方的“立即注册”(有帐号的可以直接登录)2:填写相关信息(推荐使用hotmail邮箱),填写完毕后点击下方的 即可PS:国家或地区请勿选择‘中国’,否则会出现‘在你的市场中未提供...
18910 1
BING壁纸免费API接口教程
接口简介:获取每日不同的必应官方壁纸。请求地址:https://cn.apihz.cn/api/img/bingapi.php,支持POST或GET请求。需提供用户ID、用户KEY及返回格式(JSON/TXT)。返回状态码和图片地址或错误信息。示例及详细文档见官网。
Bing API 将新增 Bing 空间数据服务
今天,微软为Bing API中增加了Bing空间数据服务,该服务允许客户将自己的数据上传至微软服务器并在现有的Bing Maps 授权下执行空间查询。   自2001年8月,微软就为用户推出了MapPoint Web服务–空间搜索和数据托管服务,如今的Bing空间数据服务正式替代了它。
2056 0
必应(Bing)每日图片获取API
必应(Bing)每日图片获取API January 11, 2015 API http://lab.dobyi.com/api/bing.php 介绍 Value Description title 标题 desc 描述 url 图片地址 你们...
1968 0
1688API最新指南:商品详情接口接入与应用
本指南介绍1688商品详情接口的接入与应用,该接口可获取商品标题、价格、规格、库存等详细信息,适用于电商平台开发、数据分析等场景。接口通过商品唯一标识查询,支持HTTP GET/POST请求,返回JSON格式数据,助力开发者高效利用1688海量商品资源。
京东API接口最新指南:店铺所有商品接口的接入与使用
本文介绍京东店铺商品数据接口的应用与功能。通过该接口,商家可自动化获取店铺内所有商品的详细信息,包括基本信息、销售数据及库存状态等,为营销策略制定提供数据支持。此接口采用HTTP请求(GET/POST),需携带店铺ID和授权令牌等参数,返回JSON格式数据,便于解析处理。这对于电商运营、数据分析及竞品研究具有重要价值。
下一篇
oss创建bucket
AI助理

你好,我是AI助理

可以解答问题、推荐解决方案等