Bing API有两种调用方式

  1. 使用Bing的Web Service
  2. 使用Azure datamarket上开放的API

Bing API服务引用

在VS中可以直接添加Web Reference,服务根地址为:

http://api.bing.net/search.wsdl?AppID={your_app_id}&Version=2.2

添加成功后,示例代码如下(C#):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
using (BingService service = new BingService())	
{
SearchRequest request = new SearchRequest();
request.AppId = APP_ID;
request.Query = "测试查询";
request.Market = "en";
request.Version = "2.2";
request.Sources = new SourceType[] { SourceType.Web };
request.Web = new WebRequest()
{
Offset = 10, # 偏移为10
OffsetSpecified = true,
Count = 10, # 取10条结果
CountSpecified = true
};
SearchResponse response = service.Search(request);
if (response.Web != null && response.Web.Results != null)
{
foreach (WebResult wr in response.Web.Results)
{
Console.WriteLine(wr.ToString());
}
}
}

调用Azure API

可以在Azure的数据市场上搜索需要的API,搜索主要涉及到以下两个:

Search API: https://datamarket.azure.com/dataset/bing/search

Search API (Web Results Only): https://datamarket.azure.com/dataset/bing/searchweb

微软官方提供了说明文档Bing Search API – Quick Start and Code Samples,主要的流程是在这里下载服务代理代码,然后就可以以类似调用Web Service的方式调用相应的功能,示例码如下(C#):

1
2
3
4
5
var bingContainer = new Bing.BingSearchContainer(new Uri(SEARCH_WEB_ROOT));
bingContainer.Credentials = new NetworkCredential(APP_ID, APP_ID);
var webQuery = bingContainer.Web("示例查询", null, null, "en-us", null, null, null, null);
webQuery = webQuery.AddQueryOption("$top", count).AddQueryOption("$skip", offset);
List<Bing.WebResult> results = webQuery.Execute().ToList();

Bing查询参数

参见在线帮助文档