美文网首页
.net调用天气接口

.net调用天气接口

作者: WangYatao | 来源:发表于2017-06-07 13:21 被阅读61次

本文使用Asp.Net (C#)调用互联网上公开的WebServices(http://www.webxml.com.cn/WebServices/WeatherWebService.asmx)来实现天气预报,该天气预报 Web 服务,数据来源于中国气象局 http://www.cma.gov.cn/ ,数据每2.5小时左右自动更新一次,准确可靠。包括 340 多个中国主要城市和 60 多个国外主要城市三日内的天气预报数据。

效果图

Paste_Image.png
步骤 :
1 、新建web 项目,添加窗体。 Paste_Image.png
2 、 引用右键--> 添加服务引用-->高级--> 添加Web引用。

3 、 将Web接口复制到URL右边的框里-->点击输入框右边的箭头,测试连接(观察左下角看是否连接成功)--> 最右边可以更改Web引用名-->添加引用。


Paste_Image.png
前台代码
    <form id="form1" runat="server">
    <div style="width:200px;margin:0 auto;background-color:aquamarine;">
    <h3>Asp.net调用天气接口</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];
        }
    }
}

相关文章

网友评论

      本文标题:.net调用天气接口

      本文链接:https://www.haomeiwen.com/subject/jxgofxtx.html