注册 登录
编程论坛 ASP.NET技术论坛

在自己的页面中加入天气预报

FenteLi 发布于 2007-11-29 12:27, 2505 次点击
直接把下面的文件保存成aspx然后让在虚拟目录下,就可以执行了。---------------抛砖引玉。大家踊跃拍砖啊。本人练过铁头功。
<%@ Page Language="C#" AutoEventWireup="True" %>
<%@ Import Namespace="System.Drawing" %>
<%@ Import Namespace=" %>
<%@ Import Namespace=" %>
 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.
<html xmlns="http://www. >
   <script runat="server">
       public string GetWeatherHTML(string url,string City)
       {
           string strWeatherHTML = "";
           HttpWebRequest rt = null;
           HttpWebResponse rs = null;
           Stream stream = null;
           StreamReader sr = null;

           try
           {
               rt = (HttpWebRequest)WebRequest.Create(url);
               rs = (HttpWebResponse)rt.GetResponse();
               stream = rs.GetResponseStream();
               sr = new StreamReader(stream, System.Text.Encoding.Default);
               strWeatherHTML = sr.ReadToEnd();
               sr.Close();
               stream.Close();
               rs.Close();

           }
           catch(Exception ep)
           {
               return "<font size=+2 color=red face=\"隶书\">出现错误:"+ep.Message+"</font>";
           }

           int start = strWeatherHTML.IndexOf(City) + 173 + City.Length;
           int end = strWeatherHTML.IndexOf("隐私保护");

           if ((start - 173 - City.Length) <=0)
           {
               return "对不起,没有该城市的天气消息!";
           }
           string strReturn = strWeatherHTML.Substring(start, end - start);
           end = strReturn.IndexOf("</div>")+6;
           strReturn = strReturn.Substring(0, end);
           strReturn = strReturn.Replace("src=\".", "src=\"http://weather.);
           strReturn = strReturn.Replace("background=\".", "background=\"http://weather.);
           return strReturn;
       }

       protected void DropDownList1_TextChanged(object sender, EventArgs e)
       {
           Response.Write(GetWeatherHTML("http://www.,DropDownList1.Text.Trim()));
       }
  </script>
<head id="Head1" runat="server">
    <title>Weather Report</title>
</head>
<body>
   <form id="form1" runat="server">
       <asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True" OnTextChanged="DropDownList1_TextChanged">
           <asp:ListItem>北京</asp:ListItem>
           <asp:ListItem>上海</asp:ListItem>
           <asp:ListItem>重庆</asp:ListItem>
           <asp:ListItem>南京</asp:ListItem>
           <asp:ListItem>天津</asp:ListItem>
           <asp:ListItem>郑州</asp:ListItem>
           <asp:ListItem>石家庄</asp:ListItem>
           <asp:ListItem>太原</asp:ListItem>
           <asp:ListItem>哈尔滨</asp:ListItem>
           <asp:ListItem>西宁</asp:ListItem>
           <asp:ListItem>沈阳</asp:ListItem>
           <asp:ListItem>呼和浩特</asp:ListItem>
           <asp:ListItem>eee</asp:ListItem>
           <asp:ListItem>周庄</asp:ListItem>
       </asp:DropDownList>
   </form>
</body>
</html>
城市大家还可以自己加的哦。只支持省会城市。。
10 回复
#2
FenteLi2007-11-29 12:43
路过的人,请别忘了留下自己的脚印哦。谢谢。
#3
zhanghy10282007-11-29 13:38
蛮好,呵呵
#4
陌生人20072007-11-29 21:50
路过,见识一下
#5
explorer_zh2007-11-30 10:30
怎么这么多人还在用VS2003,08马上都发行了
#6
ehvu2007-12-02 23:54
回复 1# 的帖子
蛮好.收了
#7
FenteLi2007-12-05 15:54
没有人顶那就 自己顶一下。
#8
chengyukyo12007-12-06 20:02
雷电之夏米
#9
清水香里2007-12-06 21:31
#10
卡卡艾2007-12-06 21:42
顶一下是应该的。
#11
xinyi_20092010-06-12 22:13
嗯!好,顶你一下!
1