本文使用Asp.Net (C#)調(diào)用互聯(lián)網(wǎng)上公開的WebServices(http://www.webxml.com.cn/WebServices/WeatherWebService.asmx)來實(shí)現(xiàn)天氣預(yù)報,該天氣預(yù)報 Web 服務(wù),數(shù)據(jù)來源于中國氣象局 http://www.cma.gov.cn/ ,數(shù)據(jù)每2.5小時左右自動更新一次,準(zhǔn)確可靠。包括 340 多個中國主要城市和 60 多個國外主要城市三日內(nèi)的天氣預(yù)報數(shù)據(jù)。
效果圖

Paste_Image.png
步驟 :
1 、新建web 項(xiàng)目,添加窗體。

Paste_Image.png
2 、 引用右鍵--> 添加服務(wù)引用-->高級--> 添加Web引用。



3 、 將Web接口復(fù)制到URL右邊的框里-->點(diǎn)擊輸入框右邊的箭頭,測試連接(觀察左下角看是否連接成功)--> 最右邊可以更改Web引用名-->添加引用。



Paste_Image.png
前臺代碼
<form id="form1" runat="server">
<div style="width:200px;margin:0 auto;background-color:aquamarine;">
<h3>Asp.net調(diào)用天氣接口</h3>
<h5>請輸入城市名稱</h5>
<input type="text" id="cityname" runat="server" style="display:block;width:200px;box-sizing:border-box;"/><span>比如:北京</span>
<asp:button runat="server" style="display:block;" ID="query" Text="查詢" OnClick="query_Click" width="200px"/>
<p id="weather_display" runat="server"></p>
</div>
</form>
后臺代碼
namespace weather
{
public partial class Home :Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void query_Click(object sender, EventArgs e)
{
WeatherService.WeatherWebService weather = new WeatherService.WeatherWebService();
string[] content = new string[23];
string _cityname = cityname.Value.Trim();
content = weather.getWeatherbyCityName(_cityname);
weather_display.InnerHtml = _cityname + ":" + content[5];
}
}
}