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

首頁 > 學院 > 開發設計 > 正文

在ASP.NET Atlas中調用Web Service—創建Mashup調用遠端Web Service(Yahoo!天氣實例)

2019-11-18 17:12:12
字體:
來源:轉載
供稿:網友

作者:Dflying Chen (http://dflying.VEVb.com/
在前一篇文章(在asp.net Atlas中調用Web Service——創建Mashup調用遠端Web Service(基礎知識以及簡單示例))中,我介紹了一些Atlas中對遠程Web Service進行Mashup的基礎知識,并給出了一個最基礎的沒有絲毫用處例子。今天再回到這個話題上,我將給出一個更復雜點的,但有一些用處的例子——Yahoo! Weather。

廢話到此為止,讓我們先熟悉一下Yahoo! Weather服務:Yahoo!在其網站上提供了天氣預報服務(http://weather.yahoo.com/),并且它也提供了Web Service的接口(http://developer.yahoo.com/weather/
從上面兩個網頁上面,我們可以知道Yahoo!提供的天氣Service的URL為http://xml.weather.yahoo.com/forecastrss,該服務還有兩個參數:

p:要查詢天氣的地點代碼(可以在http://weather.yahoo.com/查詢到不同地方的這個代碼)。
u:返回結果中溫度的單位,f代表華氏度,c代表攝氏度。
看來這個Yahoo! Weather服務還挺簡單的,讓我們測試下好不好用。先到http://weather.yahoo.com/查出來上海的地點代碼為CHXX0116。然后在瀏覽器中輸入http://xml.weather.yahoo.com/forecastrss?p=CHXX0116&u=c,嗯,返回了如下的一段不是很復雜的XML:

Yahoo Weather Service XML Result
<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
<rss version="2.0" xmlns:yweather="    <yweather:units temperature="C" distance="km"     <yweather:wind chill="21" direction="260" speed="14" />
    <yweather:atmosphere humidity="78" visibility="299" pressure="0" rising="0" />
    <yweather:astronomy sunrise="4:52 am" sunset="6:50 pm" />
    <image>
      <title>Yahoo! Weather</title>
      <width>142</width>
      <height>18</height>
      <link>http://weather.yahoo.com/</link>
      <url>http://us.i1.yimg.com/us.yimg.com/i/us/nws/th/main_142b.gif</url>
    </image>
    <item>
      <title>Conditions for Shanghai, CH at 11:00 am CST</title>
      <geo:lat>31.17</geo:lat>
      <geo:long>121.43</geo:long>
      <link>http://us.rd.yahoo.com/dailynews/rss/weather/Shanghai__CH/*http://xml.weather.yahoo.com/forecast/CHXX0116_c.html</link>
      <pubDate>Thu, 25 May 2006 11:00 am CST</pubDate>
      <yweather:condition text="Fog" code="20" temp="21" date="Thu, 25 May 2006 11:00 am CST" />
      <description>
        <![CDATA[
          <img src="
           <b>Current Conditions:</b><br />
           Fog, 21 C<BR /><BR />
           <b>Forecast:</b><BR />
            Thu - Scattered Thunderstorms. High: 25 Low: 20<br />
            Fri - AM Showers. High: 26 Low: 18<br />
           <br />
          <a href="
Forecast at Yahoo! Weather</a><BR/>
           (provided by The Weather Channel)<br/>
         ]]>
      </description>
      <yweather:forecast day="Thu" date="25 May 2006" low="20" high="25" text="Scattered Thunderstorms" code="38" />
      <yweather:forecast day="Fri" date="26 May 2006" low="18" high="26" text="AM Showers" code="39" />
      <guid isPermaLink="false">CHXX0116_2006_05_25_11_0_CST</guid>
    </item>
  </channel>
</rss>
<!-- p1.weather.scd.yahoo.com uncompressed/chunked Thu May 25 20:49:07 PDT 2006 -->

我們可以看到,它提供的信息非常全面(連日出日落時間都有……),下面讓我們書寫asbx Bridge頁面來對這個Service進行Mashup。

首先,參考在ASP.NET Atlas中調用Web Service——創建Mashup調用遠端Web Service(基礎知識以及簡單示例)這篇文章中的那個asbx的聲明,我們可以寫出如下一段:

<?xml version="1.0" encoding="utf-8" ?>
<bridge namespace="Dflying" className="YahooWeatherService">

  <proxy type="Microsoft.Web.Services.BridgeRestProxy"
         serviceUrl="

  <method name="GetWeather">
    <input>
      <parameter name="p" />
      <parameter name="u" value="c" />
    </input>
  </method>
</bridge>

 

其中:
<bridge>的namespace和className屬性以及<method>的name屬性讓我們在客戶端
javaScript中可以通過Dflying.YahooWeatherService.GetWeather()這樣的方法簽名來訪問這個Mashup。
<proxy>的serviceUrl屬性指定了Yahoo! Weather Service的URL。
GetWeather方法中定義了上面列出來的p和u兩個參數,其中u參數我們指定了它的默認值為c(代表攝氏度),p參數將由調用者負責傳過來。
寫到這一步其實也夠了,客戶端將收到上面瀏覽器中看到的那一段XML String,并且可以在客戶端進行處理并顯示。但客戶端對XML的處理并不是那么容易,也不是那么高效,同時通過網絡傳輸太多不必要的信息也是一種浪費。所以這里我們利用asbx中內建的Transformer對這段XML處理一下,提取出我們感興趣的內容并以JSON的形式發給客戶端。在<method>段中加入下面一段:

<transforms>
  <transform type="Microsoft.Web.Services.XPathBridgeTransformer">
    <data>
      <attribute name="selector" value="channel" />
      <dictionary name="namespaceMapping">
        <item name="yweather" value="      </dictionary>
      <dictionary name="selectedNodes">
        <item name="Title" value="title" />
        <item name="Description" value="item/description" />
        <item name="CurrentCondition" value="item/yweather:condition/@text" />
      </dictionary>
    </data>
  </transform>
</transforms>

 

其中<transforms>聲明表示這個Mashup方法的返回值將會被一些transformer改變一下,里面聲明了一個類型為Microsoft.Web.Services.XPathBridgeTransformer的transformer,表示將用XPath表達式來轉換。在這個XPathBridgeTransformer中要聲明如下部分:
name為selector的一個attribute段,其中指定的value屬性為一個XPath表達式,將選取整個XPathBridgeTransformer將用到的數據段。
name為namespaceMapping的一個dictionary段,其中指定了這個XML文件中的namespace映射。如果在下面的選擇節點過程中我們用到了某個namespace,那么這里就必須有它的聲明。這里我們在其中添加一個對yweather的映射,因為下面要用到。
name為selectedNodes的一個dictionary段,其中每一個item的value屬性是一個XPath String,用來從XML中選擇出相應的值,name屬性用來指定相應的在
Javascript中的屬性名稱。這里作為示例,我只取得其中三段內容,您可以看到,其中CurrentCondition的XPath中用到了上面指定的namespaceMapping。
關于XPath的知識,我就不多講了,感興趣或是不太熟悉的朋友可以自行Google,網上資源很多。關于其他類型的Transformer,我也不是很熟悉,今后如果遇到了我再講講。完成后的YahooWeatherBridge.asbx文件如下:

<?xml version="1.0" encoding="utf-8" ?>
<bridge namespace="Dflying" className="YahooWeatherService">

  <proxy type="Microsoft.Web.Services.BridgeRestProxy"
         serviceUrl="

  <method name="GetWeather">
    <input>
      <parameter name="p" />
      <parameter name="u" value="c" />
    </input>
    <transforms>
      <transform type="Microsoft.Web.Services.XPathBridgeTransformer">
        <data>
          <attribute name="selector" value="channel" />
          <dictionary name="namespaceMapping">
            <item name="yweather" value="
          </dictionary>
          <dictionary name="selectedNodes">
            <item name="Title" value="title" />
            <item name="Description" value="item/description" />
            <item name="CurrentCondition" value="item/yweather:condition/@text" />
          </dictionary>
        </data>
      </transform>
    </transforms>
  </method>
</bridge>

 

現在創建一個ASP.NET Page測試一下,首先依然是重復了一千遍的ScriptManager,還有對Bridge的引用: <atlas:ScriptManager ID="sm" runat="server">
    <Services>
        <atlas:ServiceReference Path="YahooWeatherBridge.asbx" />
    </Services>
</atlas:ScriptManager>
然后一個HTML Select元素,里面列入了幾個城市以及相應的城市代碼:

<!-- place selector -->
<select id="place">
    <option selected="selected" value="CHXX0116">Shanghai, CH</option>
    <option value="USCA0746">Mountain View, CA</option>
    <option value="CHXX0008">Beijing, CH</option>
</select>
一個HTML Button,用來觸發對Service的調用:

<!-- invoke the service -->
<input id="getWeather" type="button" value="Get Weather" onclick="return getWeather_onclick()" />
一段HTML用來顯示結果:

<!-- display result -->
<div id="result" style="display: none;">
    <div style="background-color: Gray; font-weight: bold;">Title</div>
    <div id="title"></div>
    <div style="background-color: Gray; font-weight: bold;">Description</div>
    <div id="description"></div>
</div>

 

然后是JavaScript,可以看到通過Dflying.YahooWeatherService.GetWeather()調用了Mashup,并在方法返回后把經過transform的值輸出到了頁面上: function getWeather_onclick() {
    // new atlas 'Select' control
    var place = new Sys.UI.Select($('place'));
   
    // invoke the bridge method
    Dflying.YahooWeatherService.GetWeather({'p': place.get_selectedValue()}, onGetComplete);
}
function onGetComplete(result) {
    $('result').style.display = "block";
    $('title').innerHTML = result[0].Title;
    $('description').innerHTML = result[0].Description;
}


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 龙山县| 诸暨市| 名山县| 皋兰县| 扎囊县| 漾濞| 平南县| 兴义市| 孟津县| 蒙阴县| 静安区| 平舆县| 美姑县| 大埔区| 银川市| 永泰县| 双鸭山市| 吴旗县| 台江县| 南陵县| 湘潭市| 健康| 广宁县| 襄城县| 凤城市| 汉川市| 赞皇县| 招远市| 平昌县| 监利县| 泰来县| 临澧县| 聊城市| 桂林市| 汝城县| 清水县| 新巴尔虎右旗| 海兴县| 平乐县| 平乐县| 漯河市|