用C#读取sina天气预报到wap页面(二)(原创)

public class weather : System.Web.UI.MobileControls.MobilePage
{
protected System.Web.UI.MobileControls.Label Label1;
protected System.Web.UI.MobileControls.Label Label2;
protected System.Web.UI.MobileControls.SelectionList s_weather;
protected System.Web.UI.MobileControls.Label l_date;
protected System.Web.UI.MobileControls.Label l_city;
protected System.Web.UI.MobileControls.Label l_wea;
protected System.Web.UI.MobileControls.Label l_sky;
protected System.Web.UI.MobileControls.Label l_w1;
protected System.Web.UI.MobileControls.Label l_w2;
protected System.Web.UI.MobileControls.Label l_w3;
protected System.Web.UI.MobileControls.Link Link1;
protected System.Web.UI.MobileControls.Command Command1;
protected System.Web.UI.MobileControls.Label Label3;
protected System.Web.UI.MobileControls.Form Form1;

private void Page_Load(object sender, System.EventArgs e)
{
// 在此处放置用户代码以初始化页面
if (!Page.IsPostBack)
{
string city = "深圳";
int start,stop;
string weather1,weather2,wea;
string wea_city = weather_city(city);
wea_city = wea_city.Replace(" ","");

start = wea_city.IndexOf("

1<b>",0,wea_city.Length);   
2stop = wea_city.IndexOf("</b>

", start);
weather1 = wea_city.Substring(start, stop-start).Trim() + " ";
weather1 = weather1.Substring(3,8).Trim();

start = wea_city.IndexOf("

  1<tdstyle=\"font-size:40px;font-family:timesnewroman;font-weight:bold;\">",0,wea_city.Length);   
  2stop = wea_city.IndexOf("℃",start) + 40;   
  3weather2 = wea_city.Substring(start, stop-start);   
  4weather2 = weather2.Substring(stop-start-42,40).Trim();   
  5weather2 = weather2.Replace("\t",""); 
  6
  7start = wea_city.IndexOf("<fontcolor=#183888><b>", 0, wea_city.Length);   
  8stop = wea_city.IndexOf("</b>",start);   
  9wea = wea_city.Substring(start,stop-start);   
 10wea = wea.Substring(22,wea.Length-22) + "kbrk";   
 11wea = wea.Replace("\t", "");   
 12wea = wea.Replace("&gt;", "k");   
 13wea = wea.Replace("&lt;", "k");   
 14wea = wea.Replace("kbrk", "k");   
 15string [] wall = null;   
 16char[] seperator = {'k'};   
 17wall = wea.Split(seperator); 
 18
 19//////////////////////////////////////   
 20l_city.Text = "[城市]:" + city;   
 21l_wea.Text = "[天气]:" + weather1;   
 22l_sky.Text = "[温度]:" + weather2;   
 23///////   
 24l_date.Text = wall[0];   
 25l_w1.Text = wall[1];   
 26l_w2.Text = wall[2];   
 27l_w3.Text = wall[3];   
 28}   
 29} 
 30
 31#region Web 窗体设计器生成的代码   
 32override protected void OnInit(EventArgs e)   
 33{   
 34//   
 35// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。   
 36//   
 37InitializeComponent();   
 38base.OnInit(e);   
 39} 
 40
 41/// <summary>   
 42/// 设计器支持所需的方法 - 不要使用代码编辑器修改   
 43/// 此方法的内容。   
 44/// </summary>   
 45private void InitializeComponent()   
 46{   
 47this.Command1.Click += new System.EventHandler(this.Command1_Click);   
 48this.Load += new System.EventHandler(this.Page_Load); 
 49
 50}   
 51#endregion 
 52
 53  
 54private void Command1_Click(object sender, System.EventArgs e)   
 55{   
 56string city = s_weather.Selection.Value.Trim();   
 57int start,stop;   
 58string weather1,weather2,wea;   
 59string wea_city = weather_city(city);   
 60wea_city = wea_city.Replace(" ",""); 
 61
 62start = wea_city.IndexOf("<b>",0,wea_city.Length);   
 63stop = wea_city.IndexOf("</b>", start);   
 64weather1 = wea_city.Substring(start, stop-start).Trim() + " ";   
 65weather1 = weather1.Substring(3,8).Trim();   
 66  
 67start = wea_city.IndexOf("<tdstyle=\"font-size:40px;font-family:timesnewroman;font-weight:bold;\">",0,wea_city.Length);   
 68stop = wea_city.IndexOf("℃",start) + 40;   
 69weather2 = wea_city.Substring(start, stop-start);   
 70weather2 = weather2.Substring(stop-start-42,40).Trim();   
 71weather2 = weather2.Replace("\t",""); 
 72
 73start = wea_city.IndexOf("<fontcolor=#183888><b>", 0, wea_city.Length);   
 74stop = wea_city.IndexOf("</b>",start);   
 75wea = wea_city.Substring(start,stop-start);   
 76wea = wea.Substring(22,wea.Length-22) + "kbrk";   
 77wea = wea.Replace("\t", "");   
 78wea = wea.Replace("&gt;", "k");   
 79wea = wea.Replace("&lt;", "k");   
 80wea = wea.Replace("kbrk", "k");   
 81string [] wall = null;   
 82char[] seperator = {'k'};   
 83wall = wea.Split(seperator); 
 84
 85//////////////////////////////////////   
 86l_city.Text = "[城市]:" + city;   
 87l_wea.Text = "[天气]:" + weather1;   
 88l_sky.Text = "[温度]:" + weather2;   
 89///////   
 90l_date.Text = wall[0];   
 91l_w1.Text = wall[1];   
 92l_w2.Text = wall[2];   
 93l_w3.Text = wall[3];   
 94} 
 95
 96public string weather_city(string city)   
 97{   
 98string temp = null;   
 99try   
100{   
101string strURL = " http://weather.news.sina.com.cn/cgi-bin/figureWeather/search.cgi ";   
102HttpWebRequest request;   
103request = (HttpWebRequest)WebRequest.Create(strURL);   
104request.Method="POST"; //Post请求方式   
105request.ContentType="application/x-www-form-urlencoded"; //内容类型   
106string paraUrlCoded = System.Web.HttpUtility.UrlEncode("city"); //参数经过URL编码   
107paraUrlCoded = paraUrlCoded + "=" + System.Web.HttpUtility.UrlEncode(city, System.Text.Encoding.GetEncoding("GB2312"));   
108byte[] payload;   
109payload = System.Text.Encoding.GetEncoding("GB2312").GetBytes(paraUrlCoded); //将URL编码后的字符串转化为字节   
110request.ContentLength = payload.Length; //设置请求的ContentLength   
111Stream writer = request.GetRequestStream(); //获得请求流   
112writer.Write(payload,0,payload.Length); //将请求参数写入流   
113writer.Close(); //关闭请求流   
114HttpWebResponse response;   
115response = (HttpWebResponse)request.GetResponse(); //获得响应流   
116Stream s;   
117s = response.GetResponseStream();   
118StreamReader objReader = new StreamReader(s,System.Text.Encoding.GetEncoding("GB2312"));   
119string HTML = "";   
120string sLine = "";   
121int i = 0;   
122while (sLine!=null)   
123{   
124i++;   
125sLine = objReader.ReadLine();   
126if (sLine!=null)   
127HTML += sLine;   
128}   
129HTML = HTML.Replace("&lt;","&lt;");   
130HTML = HTML.Replace("&gt;","&gt;");   
131int start,stop;   
132start = HTML.IndexOf("&lt;img src=\" http://image2.sina.com.cn/dy/weather/images/figure/",0,HTML.Length );   
133stop = HTML.IndexOf("&lt;td background=http://image2.sina.com.cn/dy/weather/images",start);   
134temp = HTML.Substring(start, stop - start);   
135}   
136catch (Exception x)   
137{   
138}   
139return temp;   
140}   
141}</fontcolor=#183888></tdstyle=\"font-size:40px;font-family:timesnewroman;font-weight:bold;\"></fontcolor=#183888></tdstyle=\"font-size:40px;font-family:timesnewroman;font-weight:bold;\">
Published At
Categories with Web编程
Tagged with
comments powered by Disqus