.net调用天气接口

本文使用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];
        }
    }
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 176,131评论 25 709
  • 小应用概述:获取天气预报(9.2) 第一章 MVC开发前奏 1.1 开发环境搭建 1.1.1 操作系统和开发工具 ...
    张中华阅读 5,548评论 6 10
  • 丁香的甜香、桃子成熟了
    桐桐和妈妈阅读 1,328评论 0 0
  • 残月轻摇梅罅雪,影疏瓣叠暗香来。 琼苞玉质冰晶染,堪抵寒霜独自开。 (平水十灰 仄起不入韵)
    山上人家123阅读 2,166评论 5 19
  • 含有剧透,以及多图(23张) 开头,给了这个镜头,意味着他们开始进入这所专制、传统、纪律、反人性的,却又是所有学校...
    王文边阅读 3,034评论 0 0

友情链接更多精彩内容