Bing API初体验 z

摘要:
在Bing正式发布仅几天后,除了其功能和搜索结果之外,作为开发者,我们还关注Bing API何时发布。周末浏览MSDN网站时,我发现BingService已经推出。链接是:http://msdn.microsoft.com/en-us/library/dd900818.aspxBing提供的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,在弹出对话框的Address文本框里填入:

http://api.search.live.net/search.wsdl?AppID=yourAppId

注意: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,改天试一下。

免责声明:文章转载自《Bing API初体验 z》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇linux内核内存管理(zone_dma zone_normal zone_highmem)Java Array数组 遍历 四种方式(包含 Lambda 表达式遍历)下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

使用Jquery Ajax请求 下载压缩文件

使用第三方组件:ICSharpCode.SharpZipLib 给按钮绑定一个点击事件 后台处理: 1 public ActionResult DownZip(stringids) 2 { 3 if (string.IsNullOrEmpty(ids)) 4 return Content("请选择要操...

HTTP认证方式与https简介

HTTP认证与https简介# HTTP请求报头: Authorization    [ˌɔ:θəraɪˈzeɪʃn] HTTP响应报头: WWW-Authenticate    [ɔ:ˈθentɪkeɪt] HTTP认证是基于质询/回应(challenge/response)的认证模式。 HTTP认证 BASIC认证 BASIC认证概述 当一个客户端向...

springmvc之文件上传、下载

1、接收到的是图片的流时 //上传头像 @RequestMapping(value = "/uploadHeadSculpture", method = RequestMethod.POST) @ResponseBody public String uploadHeadSculpture(@RequestParam("photo")...

[原创]ASP.NET中Response.Redirect()方法深度剖析[转]

本文中,我们将借助http分析工具Fiddler,来对Response.Redirect()方法的整个流程进行剖析      http://www.cnblogs.com/ybwang/archive/2010/07/20/1781800.html 首先在Visual Studio中新建一个网站,在Default页面的Page_Load事件中写下Resp...

Jsp基础知识

Jsp:Java Server Pages http://127.0.0.1:8080/weba/hello.html Tomcat执行过程: 浏览器通过http协议发送请求,以TCP协议作为底层,去tomcat的安装目录下找到webapps下的weba文件夹,再继续找到hello.html. http协议有协议头和协议头,底层是TCP,是无状态的,两次连...

干货 | 10分钟玩转PWA

关于PWA PWA(Progressive Web App), 即渐进式web应用。PWA本质上是web应用,目的是通过多项新技术,在安全、性能、体验等方面给用户原生应用的体验。而且无需像原生应用那样繁琐的下载、安装、升级等操作。 这里解释下概念中的“渐进式”,意思是这个web应用还在不断地进步中。因为目前而言,PWA还没有成熟到一蹴而就的程度,想在安全、...