从中央气象台抓取天气预报
这几天郑州下雪,一直盼望晴天的到来,于是关注天气情况.
可以Google一下:tq 郑州
如今写了个抓取中央气象台的天气预报(http://www.tq121.com)
别的城市天气情况请调用相应的URL,以及修改城市名称.
可以Google一下:tq 郑州
如今写了个抓取中央气象台的天气预报(http://www.tq121.com)
public string GetWeather(string url)
{
string weacherhtml=String.Empty;
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);
weacherhtml=sr.ReadToEnd();
}
catch(Exception ee)
{
Response.Write("<script>alert('发生异常')</script>");
}
finally
{
sr.Close();
stream.Close();
rs.Close();
}
int start=weacherhtml.IndexOf("郑州");
int end=weacherhtml.IndexOf("隐私保护");
return weacherhtml.Substring(start,end-start);
}
{
string weacherhtml=String.Empty;
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);
weacherhtml=sr.ReadToEnd();
}
catch(Exception ee)
{
Response.Write("<script>alert('发生异常')</script>");
}
finally
{
sr.Close();
stream.Close();
rs.Close();
}
int start=weacherhtml.IndexOf("郑州");
int end=weacherhtml.IndexOf("隐私保护");
return weacherhtml.Substring(start,end-start);
}
别的城市天气情况请调用相应的URL,以及修改城市名称.