using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Net;
using System.IO;
namespace WebApplication1
{
/// <summary>
/// Summary description for WebForm1.
/// </summary>
public class WebForm1 : System.Web.UI.Page
{
protected System.Web.UI.WebControls.Label l_date;
protected System.Web.UI.WebControls.Label l_city;
protected System.Web.UI.WebControls.Label l_wea;
protected System.Web.UI.WebControls.Label l_sky;
protected System.Web.UI.WebControls.Label l_w1;
protected System.Web.UI.WebControls.Label l_w2;
protected System.Web.UI.WebControls.Button Button1;
protected System.Web.UI.WebControls.Label Label1;
protected System.Web.UI.WebControls.Label l_w3;
protected System.Web.UI.WebControls.Label l_w4;
protected System.Web.UI.WebControls.DropDownList ddlcity;
private void Page_Load(object sender, System.EventArgs e)
{
if (!Page.IsPostBack)
{
writeWeatherInfo(ddlcity.SelectedValue);
writeWeather(ddlcity.SelectedValue);
}
}
#region getWeatherByCity 通過城市過慮
public string getWeatherByCity(string city)
{
string temp = null;
try
{
string strURL = "http://weather.news.sina.com.cn/cgi-bin/figureWeather/search.cgi";
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("<","<");
HTML = HTML.Replace(">",">");
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);
}
catch (Exception x)
{
}
return temp;
}
#endregion
#region writeWeatherInfo 顯示處理後的信息
private void writeWeatherInfo(string city)
{
int start,stop;
string weather1,weather2,wea;
string wea_city = getWeatherByCity(city);
wea_city = wea_city.Replace(" ","");
start = wea_city.IndexOf("<b>",0,wea_city.Length);
stop = wea_city.IndexOf("</b>", start);
weather1 = wea_city.Substring(start, stop-start).Trim() + " ";
weather1 = weather1.Substring(3,8).Trim();
start = wea_city.IndexOf("<tdstyle=/"font-size:40px;font-family:TimesNewRoman;font-weight:bold;/">",0,wea_city.Length);
stop = wea_city.IndexOf("℃",start) + 40;
weather2 = wea_city.Substring(start, stop-start);
weather2 = weather2.Substring(stop-start-42,40).Trim();
weather2 = weather2.Replace("/t","");
start = wea_city.IndexOf("<fontcolor=#183888><b>", 0, wea_city.Length);
stop = wea_city.IndexOf("</b></font>",start);
wea = wea_city.Substring(start,stop-start);
wea = wea.Substring(22,wea.Length-22) + "kbrk";
wea = wea.Replace("/t", "");
wea = wea.Replace(">", "k");
wea = wea.Replace("<", "k");
wea = wea.Replace("kbrk", "k");
string [] wall = null;
char[] seperator = {'k'};
wall = wea.Split(seperator);
l_city.Text = "[城市]:" + city;//城市
l_wea.Text = "[天氣]:" + weather1;//天氣
l_sky.Text = "[溫度]:" + weather2;//溫度
l_date.Text = wall[0];//日期
l_w1.Text = wall[1];//風向
l_w2.Text = wall[2];//風力
l_w3.Text = wall[3]; //空氣質量
l_w4.Text = wall[4]; //紫外線強度
}
#endregion
#region writeWeather 直接顯示讀取到的信息
private void writeWeather(string city)
{
string wea_city = getWeatherByCity(city);
Response.Write(wea_city);
}
#endregion
#region Button1_Click
private void Button1_Click(object sender, System.EventArgs e)
{
writeWeatherInfo(ddlcity.SelectedValue);
writeWeather(ddlcity.SelectedValue);
}
#endregion
#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: This call is required by the ASP.NET Web Form Designer.
//
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.Button1.Click += new System.EventHandler(this.Button1_Click);
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
}
}
源碼:http://singlepine.cnblogs.com/Files/singlepine/Weather.rar