华域联盟 .Net ASP.NET使用WebService实现天气预报功能

ASP.NET使用WebService实现天气预报功能

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

效果图 :

步骤 :

1 、新建web 项目,添加窗体。

2 、 引用右键--> 添加服务引用-->高级--> 添加Web引用。

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

前台代码

<div>
 
 <br />
 <br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Asp.Net 调用WebService实现天气预报<br />
 <br />
 <br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 请输入城市名称:<asp:TextBox ID="txtcity" runat="server"></asp:TextBox>
&nbsp;&nbsp;
 <asp:Label ID="Label1" runat="server" style="color: red" BorderColor="Red" Text="如 :上海"></asp:Label>
 <br />
 <br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
 <asp:Button ID="btncheck" runat="server" Text="查询" Width="69px" OnClick="btncheck_Click" />
&nbsp;
 <br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 天气概况 : <asp:Label ID="lbtianqi" runat="server" style="" BorderColor="Red" Text=""></asp:Label>
 <br />
 <br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 天气实况 :&nbsp;&nbsp; 
 <br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
 <asp:TextBox ID="txtcityweather" runat="server" Height="62px" TextMode="MultiLine" Width="258px"></asp:TextBox>
&nbsp;&nbsp;
 <br />
 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
 <br />
 <br />
 <br />
 <br />
 <br />
 <br />
 
 </div>

后台代码

protected void btncheck_Click(object sender, EventArgs e)
 {
  WeatherService.WeatherWebService w = new WeatherService.WeatherWebService();
  string [] res=new string[23];
  string cityname = txtcity.Text.Trim();
  res = w.getWeatherbyCityName(cityname);
  lbtianqi.Text = cityname + " "+res[6];
  txtcityweather.Text = res[10];

 }

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持华域联盟。

您可能感兴趣的文章:

  • jQuery调用Webservice传递json数组的方法
  • 甩掉ashx和asmx使用jQuery.ajaxWebService请求WebMethod简练处理Ajax
  • 使用jQuery Ajax 请求webservice来实现更简练的Ajax
  • C# WebService发布以及IIS发布
  • jQuery 调用WebService 实例讲解
  • ASP.NET中基于soaphead的webservice安全机制
  • WebService的相关概念

本文由 华域联盟 原创撰写:华域联盟 » ASP.NET使用WebService实现天气预报功能

转载请保留出处和原文链接:https://www.cnhackhy.com/70470.htm

本文来自网络,不代表华域联盟立场,转载请注明出处。

作者: sterben

发表回复

联系我们

联系我们

2551209778

在线咨询: QQ交谈

邮箱: [email protected]

工作时间:周一至周五,9:00-17:30,节假日休息

关注微信
微信扫一扫关注我们

微信扫一扫关注我们

关注微博
返回顶部