【用C#讀取sina天氣預報到wap頁面】

頁面代碼:

<%@ Register TagPrefix="mobile" Namespace="System.Web.UI.MobileControls" Assembly="System.Web.Mobile" %><%@ Page language="c#" Codebehind="weather.aspx.cs" Inherits="goodwap.life.weather" AutoEventWireup="false" %><mobile:form id="Form1" runat="server">
<FONT face="宋體">
<mobile:Label id="Label3" runat="server" Font-Bold="True">【生活資訊】</mobile:Label>
<mobile:Label id="Label1" runat="server" Font-Italic="True">==天氣預報==</mobile:Label>
<mobile:Label id="l_date" runat="server"></mobile:Label>
<mobile:Label id="l_city" runat="server"></mobile:Label>
<mobile:Label id="l_wea" runat="server"></mobile:Label>
<mobile:Label id="l_sky" runat="server"></mobile:Label>
<mobile:Label id="l_w1" runat="server"></mobile:Label>
<mobile:Label id="l_w2" runat="server"></mobile:Label>
<mobile:Label id="l_w3" runat="server"></mobile:Label>
<mobile:Label id="Label2" runat="server">【請選擇城市名】:</mobile:Label>
<mobile:SelectionList id="s_weather" runat="server">
<Item Value="深圳" Text="深圳" Selected="True"></Item>
<Item Value="廣州" Text="廣州"></Item>
<Item Value="上海" Text="上海"></Item>
<Item Value="北京" Text="北京"></Item>
<Item Value="天津" Text="天津"></Item>
<Item Value="武漢" Text="武漢"></Item>
<Item Value="重慶" Text="重慶"></Item>
<Item Value="成都" Text="成都"></Item>
<Item Value="南京" Text="南京"></Item>
<Item Value="香港" Text="香港"></Item>
<Item Value="澳門" Text="澳門"></Item>
</mobile:SelectionList>
<mobile:Command id="Command1" runat="server">查 詢</mobile:Command>
<mobile:Link id="Link1" runat="server" NavigateUrl="../default.aspx#Goodview">首 頁</mobile:Link></FONT></mobile:form>

後臺代碼:

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("<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];
   }
  }

  #region Web 窗體設計器生成的代碼
  override protected void OnInit(EventArgs e)
  {
   //
   // CODEGEN: 該調用是 ASP.NET Web 窗體設計器所必需的。
   //
   InitializeComponent();
   base.OnInit(e);
  }

  /// <summary>
  /// 設計器支持所需的方法 - 不要使用代碼編輯器修改
  /// 此方法的內容。
  /// </summary>
  private void InitializeComponent()
  {   
   this.Command1.Click += new System.EventHandler(this.Command1_Click);
   this.Load += new System.EventHandler(this.Page_Load);

  }
  #endregion


  private void Command1_Click(object sender, System.EventArgs e)
  {
   string city = s_weather.Selection.Value.Trim();
   int start,stop;
   string weather1,weather2,wea;
   string wea_city = weather_city(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];  
  }

  public string weather_city(string city)
  {
   string temp = null;
   try
   {
    string strURL = "http://weather.news.sina.com.cn/cgi-bin/figureWeather/search.cgi";

    //string strURL = "http://php.weather.sina.com.cn/search.php?city="+city+"";
    HttpWebRequest request;
    request = (HttpWebRequest)WebRequest.Create(strURL);
    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;
    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;
    response = (HttpWebResponse)request.GetResponse(); //獲得響應流
    Stream s;
    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("&lt;","<");
    HTML = HTML.Replace("&gt;",">");
    int start,stop;
    start = HTML.IndexOf("<img src=/"http://image2.sina.com.cn/dy/weather/images/figure/",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;
  }
 }

發佈了41 篇原創文章 · 獲贊 1 · 訪問量 8萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章