本文使用web (C#)調用互聯網上公開的WebServices接口:
(http://www.webxml.com.cn/WebServices/WeatherWebService.asmx)來實現天氣預報,該天氣預報 Web 服務,
數據來源於中國氣象局 http://www.cma.gov.cn/ ,數據每2.5小時左右自動更新一次,准確可靠。包括 340 多個中國主
要城市和 60 多個國外主要城市三日內的天氣預報數據。
前端顯示效果圖:
實現的步驟:
1、引入Web服務。在VS中項目上右擊→添加服務引用。
2、在彈出的添加服務引用窗口,加入web服務地址和引用后的命名空間。
3、在后台頁面拼湊前端顯示的內容的字符串,實現代碼。核心代碼如下:
1 public string getWeather() 2 { 3 StringBuilder sb = new StringBuilder(); 4 string[] WI = new string[22];//聲明string數組儲存放返回結果 5 cn.com.webxml.www.WeatherWebService s = new cn.com.webxml.www.WeatherWebService(); 6 string CityName = "北京"; 7 WI = s.getWeatherbyCityName(CityName); 8 sb.Append(string.Format(@"<table> 9 <tr><td>今天天氣概況:</td><td><span>{0}</span> <span><img src=""image/weather/{1}""/> <img src=""image/weather/{2}""/></span></td></tr> 10 <tr><td>今天天氣實況:</td><td>{3}</td></tr> 11 <tr><td>明天天氣概況:</td><td><span>{4}</span> <span><img src=""image/weather/{5}""/> <img src=""image/weather/{6}""/></span></td></tr> 12 <tr><td>后天天氣概況:</td><td><span>{7}</span> <span><img src=""image/weather/{8}""/> <img src=""image/weather/{9}""/></span></td></tr> 13 </table>",WI[6],WI[8],WI[9],WI[10],WI[13],WI[15],WI[16],WI[18],WI[20],WI[21])); 14 return sb.ToString(); 15 }
4、綁定字符串到web前端頁面:
<%=getWeather() %>
4、天氣的圖標可至【http://www.webxml.com.cn/images/weather.zip】下載。
5、Web服務的其他方法參數直接訪問【http://www.webxml.com.cn/WebServices/WeatherWebService.asmx】查詢,從而實現其它豐富功能。
個人博客地址http://www.cnblogs.com/mengfei001/