Web Service学习笔记:天气预报Web服务的使用方法

学了一段时间的Web服务,今天做了一个Web服务,利用YAHOO的公开天气API做自己的Web服务,主要是想练练手。现在把过程和心得分享给大家。

 

文章在我小站的地址:Web Service学习笔记:天气预报Web服务的使用方法

 

前天写了一篇文章,Web Service学习笔记:利用YAHOO公开API做天气预报Web服务 (对于本文中有不理解的地方,请参见本文),今天我就来说说如何使用Web服务(以使用这个天气服务为例子)。

 

首先要知道Web服务的地址:http://www.h2bbs.com/Weather/weather.asmx

 

下面就来说说使用的过程:

(1)首先创建一个WebApplication,做个前台页面,如下图:



 

为了方便大家,把代码也贴出来,前台代码如下:

 

< html xmlns = " http://www.w3.org/1999/xhtml "   >
< head runat = " server " >
    
< title > 天气预报 </ title >
    
< style type = " text/css " >
ExpandedBlockStart.gifContractedBlock.gif        table
{clear: both; width: 60%; font: 12px Verdana; text-align: center; border:1px solid #eeeeee;}
ExpandedBlockStart.gifContractedBlock.gif        .titletd
{padding: 5px; color: #000000; height:24px; font-weight:bold; background-color:#eeeeee;}
ExpandedBlockStart.gifContractedBlock.gif        th
{height:26px; padding: 7px 5px 6px 5px; color: #ffffff; background: #456285;}
ExpandedBlockStart.gifContractedBlock.gif        .itemtd
{padding: 5px; color: #000000; border-bottom: 1px solid #eeeeee; height:24px;}
ExpandedBlockStart.gifContractedBlock.gif        .altertd
{padding: 5px; color: #000000; background-color:#eeeeee; border-bottom: 1px solid #eeeeee; height:24px;}
    
</ style >
</ head >
< body >
    
< form id = " form1 "  runat = " server " >
    
< div style = " text-align:center; " >
    
< asp:Repeater ID = " rptWeather "  runat = " server " >
        
< HeaderTemplate >
            
< table cellpadding = " 0 "  cellspacing = " 0 "  border = " 0 " >
                
< tr >
                    
< td colspan = " 5 "   class = " titletd " > H2站长论坛 天气预报中心 </ td >
                
</ tr >
                
< tr >
                    
< th > 日期 </ th >
                    
< th > 星期 </ th >
                    
< th > 天气 </ th >
                    
< th > 最低温度 </ th >
                    
< th > 最高温度 </ th >
                
</ tr >
        
</ HeaderTemplate >
        
< ItemTemplate >
                 
< tr >
                    
< td  class = " itemtd " ><% # Eval( " Date " ) %></ td >
                    
< td  class = " itemtd " ><% # Eval( " Week " ) %></ td >
                    
< td  class = " itemtd " ><% # Eval( " Weather " ) %></ td >
                    
< td  class = " itemtd " ><% # Eval( " Tlow " ) %></ td >
                    
< td  class = " itemtd " ><% # Eval( " Thigh " ) %></ td >
                
</ tr >                
        
</ ItemTemplate >
        
< AlternatingItemTemplate >
                  
< tr >
                    
< td  class = " altertd " ><% # Eval( " Date " ) %></ td >
                    
< td  class = " altertd " ><% # Eval( " Week " ) %></ td >
                    
< td  class = " altertd " ><% # Eval( " Weather " ) %></ td >
                    
< td  class = " altertd " ><% # Eval( " Tlow " ) %></ td >
                    
< td  class = " altertd " ><% # Eval( " Thigh " ) %></ td >
                
</ tr >                
        
</ AlternatingItemTemplate >
        
< FooterTemplate >      
            
</ table >
        
</ FooterTemplate >
    
</ asp:Repeater >
    
< table >
            
< tr >
                
< td  class = " itemtd " >
                    请选择要查询的城市:
                
</ td >
                
< td colspan = " 2 "   class = " itemtd " >
                    
< asp:DropDownList ID = " ddlSelectCity "  runat = " server "  AutoPostBack = " True "  onselectedindexchanged = " ddlSelectCity_SelectedIndexChanged " >
                        
< asp:ListItem Selected = " True "  Value = " None " > 请选择要查询的城市 </ asp:ListItem >
                        
< asp:ListItem Value = " 0008 " > 北京 </ asp:ListItem >
                        
< asp:ListItem Value = " 0133 " > 天津 </ asp:ListItem >
                        
< asp:ListItem Value = " 0044 " > 杭州 </ asp:ListItem >
                        
< asp:ListItem Value = " 0448 " > 合肥 </ asp:ListItem >
                        
< asp:ListItem Value = " 0116 " > 上海 </ asp:ListItem >
                        
< asp:ListItem Value = " 0031 " > 福州 </ asp:ListItem >
                        
< asp:ListItem Value = " 0017 " > 重庆 </ asp:ListItem >
                        
< asp:ListItem Value = " 0097 " > 南昌 </ asp:ListItem >
                        
< asp:ListItem Value = " 0049 " > 香港 </ asp:ListItem >                             
                    
</ asp:DropDownList >
                
</ td >
                
< td colspan = " 2 "   class = " itemtd " >
                    现在是 
< b >< asp:Label ID = " lblCityWeather "  runat = " server "  Text = " 北京 " ></ asp:Label ></ b >  的天气情况
                
</ td >
            
</ tr >
        
</ table >    
    
</ div >
    
</ form >
    
</ body >
</ html >

 

 

(2)编写后台,后台代码如下:

 

         protected   void  Page_Load( object  sender, EventArgs e)
ExpandedBlockStart.gifContractedBlock.gif        
{
            
if (!IsPostBack)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
{
                H2BBS.Weather.WeatherWebService getWeather 
= new GetWebService.H2BBS.Weather.WeatherWebService();
                rptWeather.DataSource 
= getWeather.GetWeather("北京");
                rptWeather.DataBind();
            }

        }


        
protected   void  ddlSelectCity_SelectedIndexChanged( object  sender, EventArgs e)
ExpandedBlockStart.gifContractedBlock.gif        
{
            
if (ddlSelectCity.SelectedValue == "None")
ExpandedSubBlockStart.gifContractedSubBlock.gif            
{
                Page.ClientScript.RegisterStartupScript(GetType(), 
"""<script>window.alert('请选择要查询的城市')</script>");
            }

            
else
ExpandedSubBlockStart.gifContractedSubBlock.gif            
{
                H2BBS.Weather.WeatherWebService getWeather 
= new GetWebService.H2BBS.Weather.WeatherWebService();
                rptWeather.DataSource 
= getWeather.GetWeather(ddlSelectCity.SelectedItem.ToString());
                rptWeather.DataBind();
                lblCityWeather.Text 
= ddlSelectCity.SelectedItem.ToString();
            }

        }

 

 

(3)生成网站,浏览页面,就成功了!比较简单,我也就不多废话了!

转载于:https://www.cnblogs.com/VisualStudio/archive/2008/10/16/1313001.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值