快速入门:使用 C# 调用必应自定义搜索终结点

警告

2020 年 10 月 30 日,必应搜索 API 从 Azure AI 服务迁移到必应搜索服务。 本文档仅供参考。 有关更新的文档,请参阅必应搜索 API 文档。 关于为必应搜索创建新的 Azure 资源的说明,请参阅通过 Azure 市场创建必应搜索资源

使用此快速入门了解如何从必应自定义搜索实例请求搜索结果。 虽然此应用程序是以 C# 编写的,但必应自定义搜索 API 是一种 RESTful Web 服务,与大多数编程语言兼容。 该示例的源代码可在 GitHub 上获得。

先决条件

  • 必应自定义搜索实例。 有关详细信息,请参阅快速入门:创建首个必应自定义搜索实例

  • Microsoft .NET Core

  • 任何版本的 Visual Studio 2019 或更高版本

  • 如果使用的是 Linux/MacOS,则可使用 Mono 运行此应用程序。

  • 必应自定义搜索 NuGet 包。

    若要在 Visual Studio 中安装此包,请执行以下操作:

    1. 在解决方案资源管理器中右键单击项目,然后选择“管理 NuGet 包” 。
    2. 搜索并选择“Microsoft.Azure.CognitiveServices.Search.CustomSearch”,然后安装该包。

    当你安装必应自定义搜索 NuGet 包时,Visual Studio 还将安装以下包:

    • Microsoft.Rest.ClientRuntime
    • Microsoft.Rest.ClientRuntime.Azure
    • Newtonsoft.Json

创建 Azure 资源

通过创建以下 Azure 资源之一开始使用必应自定义搜索 API。

必应自定义搜索资源

  • 在删除资源前,可通过 Azure 门户使用。
  • 使用免费定价层试用该服务,稍后升级到用于生产的付费层。

多服务资源

  • 在删除资源前,可通过 Azure 门户使用。
  • 在多个 Azure AI 服务中对应用程序使用相同的密钥和终结点。

创建并初始化应用程序

  1. 在 Visual Studio 中创建新的 C# 控制台应用程序。 然后,将以下包添加到项目:

    using System;
    using System.Net.Http;
    using System.Web;
    using Newtonsoft.Json;
    
  2. 创建以下类,用于存储必应自定义搜索 API 返回的搜索结果:

    public class BingCustomSearchResponse {        
        public string _type{ get; set; }            
        public WebPages webPages { get; set; }
    }
    
    public class WebPages {
        public string webSearchUrl { get; set; }
        public int totalEstimatedMatches { get; set; }
        public WebPage[] value { get; set; }        
    }
    
    public class WebPage {
        public string name { get; set; }
        public string url { get; set; }
        public string displayUrl { get; set; }
        public string snippet { get; set; }
        public DateTime dateLastCrawled { get; set; }
        public string cachedPageUrl { get; set; }
    }
    
  3. 在项目的 main 方法中,为必应自定义搜索 API 订阅密钥、搜索实例的自定义配置 ID 和搜索词创建以下变量:

    var subscriptionKey = "YOUR-SUBSCRIPTION-KEY";
    var customConfigId = "YOUR-CUSTOM-CONFIG-ID";
    var searchTerm = args.Length > 0 ? args[0]:"microsoft";
    
  4. 构造请求 URL,方法是:将搜索词追加​​到 q= 查询参数后面,并将搜索实例的自定义配置 ID 追加​​到 customconfig= 参数后面。 使用与号 (&) 分隔参数。 对于 url 变量值,可以使用以下代码中的全局终结点,或者使用资源的 Azure 门户中显示的自定义子域终结点。

    var url = "https://api.cognitive.microsoft.com/bingcustomsearch/v7.0/search?" +
                "q=" + searchTerm + "&" +
                "customconfig=" + customConfigId;
    

发送和接收搜索请求

  1. 创建请求客户端,并将订阅密钥添加到 Ocp-Apim-Subscription-Key 标头。

    var client = new HttpClient();
    client.DefaultRequestHeaders.Add("Ocp-Apim-Subscription-Key", subscriptionKey);
    
  2. 执行搜索请求并获取 JSON 对象形式的响应。

    var httpResponseMessage = client.GetAsync(url).Result;
    var responseContent = httpResponseMessage.Content.ReadAsStringAsync().Result;
    BingCustomSearchResponse response = JsonConvert.DeserializeObject<BingCustomSearchResponse>(responseContent);
    

处理和查看结果

  • 循环访问响应对象以显示有关每条搜索结果的信息,包括其名称、URL 和上次网页爬网的日期。

    for(int i = 0; i < response.webPages.value.Length; i++) {                
        var webPage = response.webPages.value[i];
    
        Console.WriteLine("name: " + webPage.name);
        Console.WriteLine("url: " + webPage.url);                
        Console.WriteLine("displayUrl: " + webPage.displayUrl);
        Console.WriteLine("snippet: " + webPage.snippet);
        Console.WriteLine("dateLastCrawled: " + webPage.dateLastCrawled);
        Console.WriteLine();
    }
    Console.WriteLine("Press any key to exit...");
    Console.ReadKey();
    

后续步骤