在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
除了搜索外,也提供了廣告、影片、phonebook以及翻譯等等的功能.
就讓我們開始來試看看Bing API吧:
http://msdn.microsoft.com/en-us/library/dd251020.aspx
首先,要先註冊一個AppID,每一個應用程式會對應使用一個AppID - http://bing.com/developers
透過Live Passport登入註冊,註冊成功後,就會產生一組序號(就是黑色噴墨的地方)。
接下來,我們開啟Visual Studio 2008,並開啟一個新的Web專案。
在專案中,我們加入Service Reference,並且在Address中輸入 http://api.search.live.net/search.wsdl?AppID您申請的AppID
按下Go按鈕之後,就可以看到LiveSearchPortType服務。
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。
然後,我們參考MSDN上的sample code,做了一些調整:
其中有些是必填屬性,有些是選擇性: http://msdn.microsoft.com/en-us/library/dd250898.aspx
Required Parameters
The following parameters are required for all requests:
- SearchRequest.AppId Property (Bing, Version 2.0)
- SearchRequest.Query Property (Bing, Version 2.0)
- SearchRequest.Sources Property (Bing, Version 2.0)
Optional Parameters
The following optional parameters are applicable to this SourceType.
- SearchRequest.Version Property (Bing, Version 2.x)
- SearchRequest.Market Property (Bing, Version 2.0)
- SearchRequest.Options Property (Bing, Version 2.0)
回應的屬性欄位:Response Fields
The following response fields are common to all source types:
The following response fields are specific to the RelatedSearch SourceType:
- RelatedSearchResult.Title Property (Bing, Version 2.0)
- RelatedSearchResult.Url Property (Bing, Version 2.0
當我們執行時,可以在TextBox輸入查詢條件:
查中文也可以:
如果把request.Market 改成 "zh-tw",會發現搜尋結果變多了(142000->154000)(這告訴我們要選對區域阿).
希望這篇文章對您有所幫助. :-)
沒有留言:
張貼留言