日韩无码专区无码一级三级片|91人人爱网站中日韩无码电影|厨房大战丰满熟妇|AV高清无码在线免费观看|另类AV日韩少妇熟女|中文日本大黄一级黄色片|色情在线视频免费|亚洲成人特黄a片|黄片wwwav色图欧美|欧亚乱色一区二区三区

RELATEED CONSULTING
相關咨詢
選擇下列產品馬上在線溝通
服務時間:8:30-17:00
你可能遇到了下面的問題
關閉右側工具欄

新聞中心

這里有您想知道的互聯(lián)網營銷解決方案
必應Bing API實戰(zhàn)初體驗

Bing提供的API很豐富,除了搜索外,還增加了廣告Ad、圖片、新聞、Phonebook、拼寫和視頻的搜索。而訪問協(xié)議有三種:JSON, XML和SOAP。JSON協(xié)議用于AJAX應用,XML用于Silverlight應用,SOAP用于傳統(tǒng)的.NET等強類型程序??梢姡④浽谕瞥鯝PI方面還是很有效率的。

使用Bing API的***步,是去Bing Developer Center上申請一個AppId,每個應用應該使用一個單獨的AppId。Bing Developer Center的網址是:http://bing.com/developers 。在頁面里先用Live ID登錄,然后選擇Get a new App ID,填寫一些基本信息,然后你就會得到一串很長的AppId。需要注意的是,Bing還有一個網址是http://www.bing.com/developer/,估計是為1.1版本準備的,現在還不能申請AppId。大家一定要分清楚。

接下來,我們在Visual Studio 2008里創(chuàng)建一個.NET應用。在Project菜單里選擇Add Service Reference,在彈出對話框的Address文本框里填入:

http://api.search.live.net/search.wsdl?AppID=yourAppId

注意:AppID=后要填寫你申請到的AppId.

在找到LiveSearchService的引用后,將其添加到我們的工程中。接下來,我根據PhoneBook和WebSearch兩個例子寫了DEMO,更多例子可以參考:

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

需要提醒的是,可能是文檔沒有更新,Bing API的類名稱還會發(fā)生變化。我發(fā)現在2009年6月8日導出的引用中,LiveSearchService的名稱變成了LiveSearchPortTypeClient。Web Search的代碼如下:

   privatevoid button2_Click(object sender, EventArgs e)
        {
            // LiveSearchService implements IDisposable.
            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 = "馬寧";
            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)
        {
            // Display the results header.
            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());
                Console.WriteLine();
            }
        }

.csharpcode, .csharpcode pre { font-size: small; color: black; font-family: consolas, "Courier New", courier, monospace; background-color: #ffffff; /*white-space: pre;*/ } .csharpcode pre { margin: 0em; } .csharpcode .rem { color: #008000; } .csharpcode .kwrd { color: #0000ff; } .csharpcode .str { color: #006080; } .csharpcode .op { color: #0000c0; } .csharpcode .preproc { color: #cc6633; } .csharpcode .asp { background-color: #ffff00; } .csharpcode .html { color: #800000; } .csharpcode .attr { color: #ff0000; } .csharpcode .alt { background-color: #f4f4f4; width: 100%; margin: 0em; } .csharpcode .lnum { color: #606060; }

從代碼上來看,很簡單,先創(chuàng)建一個LiveSearchPortTypeClient的對象,然后,創(chuàng)建SearchRequest對象,在Request里需要設置的是AppId,Query和Sources。AppId不用多說了,Query里填我們要查的關鍵字,Sources里指定SourceType,我們這里指定的是SourceType.Web。

將SearchRequest參數傳遞給LiveSearchPortTypeClient的Search方法,會返回一個SearchResponse的對象,里邊包含我們的搜索結果。結果會包含在response.Web.Results對象里,最主要的參數是Title、Description和Url。

***的運行結果就是這樣的了:

Bing的好壞還需要時間檢驗,但是Bing API和Google API應該差不多,而且考慮了不同用戶的需求,這也許就是軟件公司和互聯(lián)網公司不一樣的地方。同時推出的還有Bing Map API,改天試一下。

【編輯推薦】

  1. 淘寶Open API初學者入門教程
  2. 必應Bing市場份額曇花一現 亞軍頭銜只占據1天
  3. 微軟借Bing推廣Silverlight 安裝須切換背景
  4. Bing叫板Google速度對比測試—“必應”完敗
  5. Bing、Google、Yahoo三強***大比拼

責任編輯:彭凡
來源: cnblogs Bing API 體驗


本文名稱:必應Bing API實戰(zhàn)初體驗
URL網址:http://www.5511xx.com/article/dpjhsdh.html