[How to]Bing API開發初體驗

[How to]Bing API開發初體驗

image

在Bing搜尋推出後不久,Bing search service API也跟著推出了- http://msdn.microsoft.com/en-us/library/dd900818.aspx

Bing Search提供了哪些 API 功能呢?

  • Retrieve information from the Internet
  • Monetize your application with advertisements
  • Improve and enhance search requests and results
  • Find location-specific information
  • Translate terms and blocks of text 

 

image

除了搜索外,也提供了廣告、影片、phonebook以及翻譯等等的功能.

 

就讓我們開始來試看看Bing API吧:

http://msdn.microsoft.com/en-us/library/dd251020.aspx

 

首先,要先註冊一個AppID,每一個應用程式會對應使用一個AppID - http://bing.com/developers

透過Live Passport登入註冊,註冊成功後,就會產生一組序號(就是黑色噴墨的地方)。

image

 

 

接下來,我們開啟Visual Studio 2008,並開啟一個新的Web專案。

在專案中,我們加入Service Reference,並且在Address中輸入 http://api.search.live.net/search.wsdl?AppID=您申請的AppID

按下Go按鈕之後,就可以看到LiveSearchPortType服務。

image

 

Bing提供三種protocol:

 

  • JavaScript Object Notation (JSON)
  • Extended Markup Language (XML)
  • SOAP (originally an acronym for Simple Object Access Protocol)

 

http://msdn.microsoft.com/en-us/library/dd251062.aspx

 

這裡我們以SOAP來作為範例。

 

在畫面上,我們拖拉一個TextBox、一個按鈕以及一個ListBox。

image

 

然後,我們參考MSDN上的sample code,做了一些調整:

 

 protected void Button1_Click(object sender, EventArgs e)
    {
        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 = TextBox1.Text.Trim();
        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)
    {
        ListBox1.Items.Clear();
        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());
        }
    }

 

 

 

 

其中有些是必填屬性,有些是選擇性: http://msdn.microsoft.com/en-us/library/dd250898.aspx

Required Parameters

The following parameters are required for all requests:

Optional Parameters

The following optional parameters are applicable to this SourceType.

 

回應的屬性欄位:Response Fields

The following response fields are common to all source types:

The following response fields are specific to the RelatedSearch SourceType:

 

 

當我們執行時,可以在TextBox輸入查詢條件:

image

查中文也可以:

image

 

如果把request.Market 改成 "zh-tw",會發現搜尋結果變多了(142000->154000)(這告訴我們要選對區域阿).

image

 

希望這篇文章對您有所幫助. :-)

 

 

 

如果您有微軟技術開發的問題,可以到MSDN Forum發問。

如果您有微軟IT管理的問題,可以到TechNet Forum發問喔。