快速入門:使用 C# 呼叫您的 Bing 自訂搜尋端點

警告

2020 年 10 月 30 日,Bing 搜尋 API 已從 Azure AI 服務移至Bing 搜尋服務。 本文件僅供參考之用。 如需更新的文件,請參閱 Bing 搜尋 API 文件。 如需針對 Bing 搜尋建立新 Azure 資源的指示,請參閱透過 Azure Marketplace 建立 Bing 搜尋資源

使用本快速入門了解如何要求 Bing 自訂搜尋執行個體所產生的搜尋結果。 雖然此應用程式是以 C# 撰寫的,但 Bing 自訂搜尋 API 是一種與大多數程式設計語言都相容的 RESTful Web 服務。 GitHub 上有此範例的原始程式碼。

Prerequisites

  • 「Bing 自訂搜尋」執行個體。 如需詳細資訊,請參閱快速入門:建立您的第一個 Bing 自訂搜尋執行個體

  • Microsoft .NET Core

  • Visual Studio 2019 或更新版本的任何版本。

  • 如果您使用 Linux/MacOS,則可以使用 Mono 來執行此應用程式。

  • Bing 自訂搜尋 NuGet 套件。

    若要在 Visual Studio 中安裝此套件:

    1. 方案總管中以滑鼠右鍵按一下您的專案,然後選取 [管理 NuGet 套件]。
    2. 搜尋並選取 Microsoft.Azure.CognitiveServices.Search.CustomSearch,然後安裝套件。

    當您安裝 Bing 自訂搜尋 NuGet 套件時,Visual Studio 也會安裝下列套件:

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

建立 Azure 資源

藉由建立下列其中一項 Azure 資源,開始使用 Bing 自訂搜尋 API。

Bing 自訂搜尋資源

  • 您可以透過 Azure 入口網站取得該資源,直到將其刪除為止。
  • 使用免費定價層來試用服務,之後可升級至付費層以用於實際執行環境。

多服務資源

  • 您可以透過 Azure 入口網站取得該資源,直到將其刪除為止。
  • 針對您的應用程式,跨多個 Azure AI 服務使用相同的金鑰和端點。

建立應用程式並將其初始化

  1. 在 Visual Studio 中,建立新的 C# 主控台應用程式。 然後,將下列套件新增至您的專案:

    using System;
    using System.Net.Http;
    using System.Web;
    using Newtonsoft.Json;
    
  2. 建立下列類別以儲存 Bing 自訂搜尋 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. 在您專案的主要方法中,為 Bing 自訂搜尋 API 訂用帳戶金鑰、搜尋執行個體的自訂組態識別碼和搜尋字詞建立下列變數:

    var subscriptionKey = "YOUR-SUBSCRIPTION-KEY";
    var customConfigId = "YOUR-CUSTOM-CONFIG-ID";
    var searchTerm = args.Length > 0 ? args[0]:"microsoft";
    
  4. 將搜尋字詞附加至 q= 查詢參數,並將搜尋執行個體的自訂設定識別碼附加至 customconfig= 參數,以建構要求 URL。 請以 & 符號分隔參數。 對於 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();
    

後續步驟