. NET 技术讨论

学于明志,交流增加见识,讨论改变思维
  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

网页上的天气预报制作

Posted on 2006-03-02 21:04    阅读(1061)  评论(0编辑  收藏  举报
最近很多网页上都具备了查询天气情况的功能,本人感兴趣之于,也得到了一写制作天气预报的页面的方法,
现在拿出来给大家共同学习,具体代码(C#)如下:

/// <summary>
        
/// 通过城市过虑
        
/// </summary>
        
/// <param name="city">某个城市的中文名称,如:北京</param>
        
/// <returns>返回天气情况的字符串组合</returns>
            public string getWeatherByCity(string city)
            {
                
string temp = string.Empty;
                
try
                {
                    
//string strURL = "http://weather.news.sina.com.cn/cgi-bin/figureWeather/search.cgi";
                    
//********************该连接地址有可能会有所改动*********//
                    string strURL = "http://php.weather.sina.com.cn/search.php";
                    
//*****************************************************************//

                    HttpWebRequest request
= (HttpWebRequest)WebRequest.Create(strURL);//使用 WebRequest.Create 方法初始化 HttpWebRequest 的一个新实例。如果 URI 的方案是 http:// 或 https://,则 Create 将返回 HttpWebRequest 实例。
                    request.Method="POST"//Post请求方式
                    request.ContentType="application/x-www-form-urlencoded"//内容类型
                    string paraUrlCoded = System.Web.HttpUtility.UrlEncode("city"); //参数经过URL编码
                    paraUrlCoded = paraUrlCoded + "=" + System.Web.HttpUtility.UrlEncode(city, System.Text.Encoding.GetEncoding("GB2312"));
                    
byte[] payload = System.Text.Encoding.GetEncoding("GB2312").GetBytes(paraUrlCoded); //将URL编码后的字符串转化为字节 
                    request.ContentLength = payload.Length; //设置请求的ContentLength
                    Stream writer = request.GetRequestStream(); //获得请求流
                    writer.Write(payload,0,payload.Length); //将请求参数写入流
                    writer.Close(); //关闭请求流
                    HttpWebResponse response = (HttpWebResponse)request.GetResponse(); //获得响应流
                    Stream s= response.GetResponseStream();
                    StreamReader objReader 
= new StreamReader(s,System.Text.Encoding.GetEncoding("GB2312"));
                    
string HTML = "";
                    
string sLine = "";
                    
int i = 0
                    
while (sLine!=null
                    {
                        i
++;
                        sLine 
= objReader.ReadLine();
                        
if (sLine!=null
                            HTML 
+= sLine;
                    } 
                    HTML 
= HTML.Replace("&lt;","<");
                    HTML 
= HTML.Replace("&gt;",">");
                    
int start,stop;
                    
//start = HTML.IndexOf("<img src=\"http://image2.sina.com.cn/dy/weather/images/figure/",0,HTML.Length);
                    start = HTML.IndexOf("<table border=0 cellpadding=0 cellspacing=0 style=\"margin:5px;\">",0,HTML.Length);
                    stop 
= HTML.IndexOf("<td background=http://image2.sina.com.cn/dy/weather/images",start); 
                    temp 
= HTML.Substring(start, stop - start);
                    temp 
= temp.Replace("\t",string.Empty);//清除水平制表符


            
//*******************************************************
                    temp = temp.Replace("<td style","<td ");
                    temp 
= temp.Replace("<b>",string.Empty);
                    temp 
= temp.Replace("</b>",string.Empty);
                    temp 
= temp.Replace("",".");
                    temp 
= temp.Replace("",".");
                    temp 
= temp.Replace("","");
                    
//去掉其他天气情况,如风力等
                    string fengli = "风力", fengxiang = "风向";
                    
if(temp.IndexOf(fengxiang) > 0)
                    {
                        temp 
= temp.Remove(temp.LastIndexOf(fengxiang),temp.Length - temp.LastIndexOf(fengxiang));
                    }
                    
else
                    {
                        temp 
= temp.Remove(temp.LastIndexOf(fengli),temp.Length - temp.LastIndexOf(fengli));
                    }
                    temp 
= temp.Remove(temp.LastIndexOf("<br>"),temp.Length - temp.LastIndexOf("<br>"));
                    
                }
                
catch (Exception)
                {
                    temp 
= "<table><tr><td><font color=\u0022#183888\u0022>没有找到城市: " + this.ddlcity.SelectedValue.Trim() +" 的天气情况<br></td></tr></table>";
                }
                
return temp;
            }


以上是得到某个城市天气情况的函数,只要制作相应的界面然后在调用该函数就行了,该函数唯一不足的是,
读取天气的连接地址有可能会有所改动,此时该地址也要相应的变动才行;

作者:冯珺
时间:2006-03-02