Bing正式发布没几天,除了功能和搜索结果外,作为开发者来说,我们关心的还有Bing API啥时候能出。周末浏览MSDN网站时,发现Bing Service已经上线了,本文为大家做一个初步介绍。
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.
在找到LiveSearchService的引证后,将其添加到咱们的工程中。接下来,我依据PhoneBook和WebSearch两个比如写了DEMO,更多比如能够参阅:
http://msdn.microsoft.com/en-us/library/dd251066.aspx
需求提示的是,可能是文档没有更新,Bing API的类称号还会发生变化。我发现在2009年6月8日导出的引证中,LiveSearchService的称号变成了LiveSearchPortTypeClient。Web Search的代码如下:
privatevoid 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(); } }.csharpcode, .csharpcode pre { font-size: small; color: black; font-family: consolas, "Courier New", courier, monospace; background-color: #ffffff; /*white-space: pre;*/ } .csharpcode pre { margin: 0em; } .csharpcode .rem { color: #008000; } .csharpcode .kwrd { color: #0000ff; } .csharpcode .str { color: #006080; } .csharpcode .op { color: #0000c0; } .csharpcode .preproc { color: #cc6633; } .csharpcode .asp { background-color: #ffff00; } .csharpcode .html { color: #800000; } .csharpcode .attr { color: #ff0000; } .csharpcode .alt { background-color: #f4f4f4; width: 100%; margin: 0em; } .csharpcode .lnum { color: #606060; }
从代码上来看,很简单,先创立一个LiveSearchPortTypeClient的目标,然后,创立SearchRequest目标,在Request里需求设置的是AppId,Query和Sources。AppId不必多说了,Query里填咱们要查的关键字,Sources里指定SourceType,咱们这儿指定的是SourceType.Web。
将SearchRequest参数传递给LiveSearchPortTypeClient的Search办法,会回来一个SearchResponse的目标,里面包括咱们的查找成果。成果会包括在response.Web.Results目标里,最主要的参数是Title、Description和Url。
***的运转成果便是这样的了:
Bing的好坏还需求时刻查验,可是Bing API和Google API应该差不多,并且考虑了不同用户的需求,这或许便是软件公司和互联网公司不一样的当地。一起推出的还有Bing Map API,改天试一下。
【修改引荐】
- 淘宝Open API初学者入门教程
- 必应Bing市场份额稍纵即逝 亚军头衔只占有1天
- 微软借Bing推行Silverlight 装置须切换布景
- Bing叫板Google速度比照测验—“必应”完败
- Bing、Google、Yahoo三强***大比拼
知优网 » 必应Bing API实战初体验(bing api接口)