在自己的页面中加入天气预报
直接把下面的文件保存成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>
城市大家还可以自己加的哦。只支持省会城市。。