WebServic調用天氣預報服務

來源:互聯網
上載者:User

標籤:idt   port   src   bag   type   tle   textbox   分享   建立   

     在項目開發中,我們除了發布WebService提供客戶調用外,也經常需要調用一些客戶或者第三方的WebService服務,這裡就通過一個Demo來示範調用一個第三方的天氣預報服務。

1.天氣預報服務介面說明:

這裡以http://www.webxml.com.cn/WebServices/WeatherWebService.asmx這個天氣預報服務為例

2.建立空mvc項目MvcAppService

3.引入Web服務。在項目引用上右鍵,選擇“加入服務參考”,如:

4.在彈出的加入服務參考視窗單擊“進階”按鈕,如:

5.在彈出的表單中單擊“添加Web引用”按鈕,如:

6.輸入Web服務地址,修改Web引用名稱,如:

 7.從 http://www.webxml.com.cn/images/weather.zip下載天氣圖片,然後解壓檔案,將檔案夾weather複製到項目Content檔案夾下(空項目沒有Content檔案夾,自己建立)

8.添加Home控制器,和兩個GetWeather方法,實現代碼。

 public ActionResult GetWeather()        {            return View();        }        [HttpPost]        public ActionResult GetWeather(string city)        {            Weather.WeatherWebService client = new Weather.WeatherWebService();            var strlist = client.getWeatherbyCityName(city);            if(strlist[8] == "")            {                ViewBag.Msg = "暫時不支援你查詢的城市";            }            else            {                ViewBag.ImgUrl = @"/Content/weather/" + strlist[8];                ViewBag.General = strlist[1] + " " + strlist[6];                ViewBag.Actually = strlist[10];            }            return View();        }

9.添加GetWeather視圖,代碼如下:

@{    Layout = null;}<!DOCTYPE html><html><head>    <meta name="viewport" content="width=device-width" />    <title>GetWeather</title></head><body>    @using (Html.BeginForm("GetWeather", "Home", FormMethod.Post))    {        <div>請輸入查詢的城市:@Html.TextBox("city")<input type="submit" value="查詢" /></div>        <div>            天氣概況:@ViewBag.General            @if (!string.IsNullOrEmpty(ViewBag.ImgUrl as string))            {                <img src="@ViewBag.ImgUrl" />            }            <div>天氣實況:@ViewBag.Actually</div>            <div>@ViewBag.Msg</div>        </div>    }</body></html>

10.運行結果如下:

 

WebServic調用天氣預報服務

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.