国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學(xué)院 > 開發(fā)設(shè)計(jì) > 正文

在ASP.NET Atlas中調(diào)用Web Service——?jiǎng)?chuàng)建Mashup調(diào)用遠(yuǎn)端Web Service(Google Search實(shí)例)

2019-11-18 17:12:01
字體:
供稿:網(wǎng)友

作者:Dflying Chen (http://dflying.VEVb.com/
在前一篇貼子(在asp.net Atlas中調(diào)用Web Service——?jiǎng)?chuàng)建Mashup調(diào)用遠(yuǎn)端Web Service(Yahoo!天氣實(shí)例))中我介紹了使用BridgeRestPRoxy對Web Service進(jìn)行Mashup。然而,在實(shí)際開發(fā)中這種簡單的方法往往是不夠用的,我們需要書寫程序代碼來完成一些復(fù)雜邏輯。也就是使用自定義的復(fù)雜的Proxy Class,而不是Atlas內(nèi)建的那幾種加上一些asbx文件中的xml標(biāo)記。今天我們來接觸一個(gè)更復(fù)雜的例子:對Google的Search Service進(jìn)行Mashup,以學(xué)習(xí)使用自定義的Class來代理對遠(yuǎn)端Web Service的調(diào)用。

首先,讓我們了解一下Google提供的Service:Google提供給我們開發(fā)者一系列的API,您可以到http://api.google.com/查看,對于我們今天要使用的Search API,您還可以到http://api.google.com/googleapi.zip下載它的幫助文檔以及示例程序。在開始這個(gè)實(shí)例之前,我們必須到http://api.google.com/申請一個(gè)Google的License Key,并在每一次對Google的請求中包含這個(gè)Key。我大概看了一下Google的文檔,上面說每個(gè)License Key每天只允許1000個(gè)請求,這樣如果需要在大型的網(wǎng)站上使用Google的Search,恐怕要準(zhǔn)備一堆的License Key了……Google可真夠小氣的-_-b。

License Key申請好,我們就可以開始了,當(dāng)然,如果您是第一次接觸Mashup,可能還要參考一下我的這篇文章:在ASP.NET Atlas中調(diào)用Web Service——?jiǎng)?chuàng)建Mashup調(diào)用遠(yuǎn)端Web Service(基礎(chǔ)知識以及簡單示例)。

首先,使用Visual Studio自帶的wsdl.exe工具,根據(jù)Google Web Service的wsdl地址生成出調(diào)用它的C#代碼:

wsdl.exe http://api.google.com/GoogleSearch.wsdl

將生成的GoogleSearchService.cs加到我們的Web Site的App_Code目錄中。到這時(shí),我們其實(shí)就可以直接使用這個(gè)文件中的類了,其中GoogleSearchService.doGoogleSearch()就是我們需要的方法。不過觀察一下這個(gè)自動生成的亂糟糟的類,其中有好多別的方法,doGoogleSearch()方法也需要好多參數(shù),所以還是先對這個(gè)亂糟糟的文件來個(gè)包裝,封裝并簡化一下對它的調(diào)用。

在這個(gè)示例程序中,對于每條搜索結(jié)果,我們只要得到它的Title,URL以及Snippet三個(gè)字段。為了減少網(wǎng)絡(luò)流量,我們不使用GoogleSearchService.cs中自帶的搜索結(jié)果的類,而是自定義一個(gè)只包含我們需要內(nèi)容的SearchResultLite Class:

public class SearchResultLite
{
    private string _title;
    public string Title
    {
        get { return _title; }
        set { _title = value; }
    }

    private string _url;
    public string Url
    {
        get { return _url; }
        set { _url = value; }
    }

    private string _snippet;
    public string Snippet
    {
        get { return _snippet; }
        set { _snippet = value; }
    }

    public SearchResultLite()
    {
    }

    public SearchResultLite(string title, string url, string snippet)
    {
        _title = title;
        _url = url;
        _snippet = snippet;
    }
}

注意上面的SearchResultLite Class中一定要有一個(gè)默認(rèn)的無參的構(gòu)造函數(shù),并且每一個(gè)字段都要使用屬性而不是public的成員,否則Atlas在做與javaScript對象的轉(zhuǎn)換過程中會出錯(cuò)。

下面來對GoogleSearchService.doGoogleSearch()進(jìn)行包裝:

public class GoogleSearchWarpper
{
    public SearchResultLite[] Search(string lisenceKey, string query)
    {
        GoogleSearchService s = new GoogleSearchService();
        GoogleSearchResult result = s.doGoogleSearch(
            lisenceKey,
            query,
            0,
            10,
            false,
            "",
            false,
            "",
            "",
            ""
        );
        List<SearchResultLite> resultLites = new List<SearchResultLite>();
        foreach (ResultElement elem in result.resultElements)
        {
            SearchResultLite resultLite = new SearchResultLite(elem.title, elem.URL, elem.snippet);
            resultLites.Add(resultLite);
        }
        return resultLites.ToArray();
    }
}

這樣我們在調(diào)用Search方法的時(shí)候只需要兩個(gè)參數(shù)即可,并且返回的數(shù)據(jù)也沒有冗余的部分。將其存為GoogleSearchWarpper.cs。

接下來我們要在web.config文件中添加開頭申請到的License Key,在后面的步驟中會用到:

<appSettings>
    <add key="GoogleWebAPILisenceKey" value="!!input your license key here!!"/>
</appSettings>

下面來看Bridge文件GoogleSearchBridge.asbx的聲明:

<?xml version="1.0" encoding="utf-8" ?>
<bridge namespace="Dflying" className="GoogleSearch" >
  <proxy type="GoogleSearchWarpper, App_Code"  />
  <method name="Search">
    <input>
      <parameter name="lisenceKey" value="% appsettings : GoogleWebAPILisenceKey %" serverOnly="true" />
      <parameter name="query" />
    </input>
  </method>
</bridge>

注意到<proxy>段的type屬性值被指定為在App_Code中的GoogleSearchWarpper類,也就是使用我們剛剛定義的Proxy對象。對于Search的兩個(gè)參數(shù):

licenseKey的value屬性值設(shè)置為% appsettings : GoogleWebAPILisenceKey %,這是asbx文件中引入的一個(gè)新寫法,代表在運(yùn)行時(shí)它的值將被指派為web.config文件中appSettings段中key為GoogleWebAPILisenceKey的值。
query將由客戶端傳過來,代表查詢的關(guān)鍵字。
到此為止,我們可以在Atlas頁面中測試一下了,當(dāng)然第一步還是在頁面上添加ScriptManager,還有對上面Bridge的引用:

<atlas:ScriptManager ID="scriptManager" runat="server">
    <Services>
        <atlas:ServiceReference Path="GoogleSearchBridge.asbx" />
    </Services>
</atlas:ScriptManager>

在添加一段HTML,用來讓用戶輸入查詢關(guān)鍵字,引發(fā)查詢并顯示結(jié)果:

<input id="tbQuery" type="text" />
<input id="btnSearch" type="button" value="Search!" onclick="return btnSearch_onclick()" />
<div id="result">
</div>

最后,編寫Javascript,可以看到其中對Sys.StringBuilder的使用:

function btnSearch_onclick() {
    var tbQuery = new Sys.UI.TextBox($("tbQuery"));
    Dflying.GoogleSearch.Search({'query': tbQuery.get_text()}, onSearchComplete);
}

function onSearchComplete(result) {
    var sbResult = new Sys.StringBuilder();
    for (var i = 0; i < result.length; ++i) {
        sbResult.append("<hr />");
        sbResult.append("<b>" + result[i].Title + "</b><br />");
        sbResult.append("<a href=/"" + result[i].Url + "/" target=/"_blank/" >" + result[i].Url + "</a><br />");
        sbResult.append(result[i].Snippet);
    }
    $('result').innerHTML = sbResult.toString();
}

示例程序可以在此下載:http://www.survivalescaperooms.com/Files/dflying/GoogleSearchBridge.zip

注意:想運(yùn)行這個(gè)示例程序,您需要在web.config中的GoogleWebAPILisenceKey部分填入您申請好的License Key。


發(fā)表評論 共有條評論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 塔河县| 佳木斯市| 五常市| 南部县| 菏泽市| 桦川县| 中山市| 长治县| 呼图壁县| 凌源市| 林口县| 闽侯县| 广丰县| 颍上县| 阿荣旗| 故城县| 黄山市| 郑州市| 禄丰县| 嵊泗县| 余庆县| 清水河县| 遂川县| 贡觉县| 巴林右旗| 法库县| 肃南| 桐庐县| 浠水县| 牡丹江市| 二连浩特市| 香格里拉县| 依安县| 乌兰浩特市| 泰和县| 皮山县| 额尔古纳市| 虎林市| 汽车| 陵水| 丰台区|