熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> .NET編程 >> 正文

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

2022-06-13   來源: .NET編程 

  <%@ Register TagPrefix=mobile
            Namespace=SystemWebUIMobileControls
            Assembly=SystemWebMobile %><%@ Page language=c#
            Codebehind=weatheraspxcs Inherits=goodwaplifeweather
            AutoEventWireup=false %><mobile:form id=Form runat=server
            <FONT face=宋體
            <mobile:Label id=Label runat=server
            FontBold=True>【生活資訊】</mobile:Label>
            <mobile:Label id=Label runat=server
            FontItalic=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_w runat=server></mobile:Label>
            <mobile:Label id=l_w runat=server></mobile:Label>
            <mobile:Label id=l_w runat=server></mobile:Label>
            <mobile:Label id=Label 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=Command runat=server>查 詢</mobile:Command>
            <mobile:Link id=Link runat=server
            NavigateUrl=/defaultaspx#Goodview>首
            頁</mobile:Link></FONT></mobile:form>

  public class weather : SystemWebUIMobileControlsMobilePage
            {
            protected SystemWebUIMobileControlsLabel Label;
            protected SystemWebUIMobileControlsLabel Label;
            protected SystemWebUIMobileControlsSelectionList s_weather;
            protected SystemWebUIMobileControlsLabel l_date;
            protected SystemWebUIMobileControlsLabel l_city;
            protected SystemWebUIMobileControlsLabel l_wea;
            protected SystemWebUIMobileControlsLabel l_sky;
            protected SystemWebUIMobileControlsLabel l_w;
            protected SystemWebUIMobileControlsLabel l_w;
            protected SystemWebUIMobileControlsLabel l_w;
            protected SystemWebUIMobileControlsLink Link;
            protected SystemWebUIMobileControlsCommand Command;
            protected SystemWebUIMobileControlsLabel Label;
            protected SystemWebUIMobileControlsForm Form;

  private void Page_Load(object sender SystemEventArgs e)
            {
            // 在此處放置用戶代碼以初始化頁面
            if (!PageIsPostBack)
            {
            string city = 深圳;
            int startstop;
            string weatherweatherwea;
            string wea_city = weather_city(city);
            wea_city = wea_cityReplace( );

  start = wea_cityIndexOf(<b>wea_cityLength);
            stop = wea_cityIndexOf(</b> start);
            weather = wea_citySubstring(start stopstart)Trim() + ;
            weather = weatherSubstring()Trim();

  start =
            wea_cityIndexOf(<td>wea_cityLength);
            stop = wea_cityIndexOf(start) + ;
            weather = wea_citySubstring(start stopstart);
            weather = weatherSubstring(stopstart)Trim();
            weather = weatherReplace( );

  start = wea_cityIndexOf(<fontcolor=#><b>
            wea_cityLength);
            stop = wea_cityIndexOf(</b></font>start);
            wea = wea_citySubstring(startstopstart);
            wea = weaSubstring(weaLength) + kbrk;
            wea = weaReplace( );
            wea = weaReplace(> k);
            wea = weaReplace(< k);
            wea = weaReplace(kbrk k);
            string [] wall = null;
            char[] seperator = {k};
            wall = weaSplit(seperator);

  /**///////////////////////////////////////
            l_cityText = [城市] + city;
            l_weaText = [天氣] + weather;
            l_skyText = [溫度] + weather;
            /**////////
            l_dateText = wall[];
            l_wText = wall[];
            l_wText = wall[];
            l_wText = wall[];
            }
            }

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

  /**//// <summary>
            /// 設計器支持所需的方法 不要使用代碼編輯器修改
            /// 此方法的內容
            /// </summary>
            private void InitializeComponent()
            {
            thisCommandClick += new SystemEventHandler(thisCommand_Click);
            thisLoad += new SystemEventHandler(thisPage_Load);

  }
            #endregion

  private void Command_Click(object sender SystemEventArgs e)
            {
            string city = s_weatherSelectionValueTrim();
            int startstop;
            string weatherweatherwea;
            string wea_city = weather_city(city);
            wea_city = wea_cityReplace( );

  start = wea_cityIndexOf(<b>wea_cityLength);
            stop = wea_cityIndexOf(</b> start);
            weather = wea_citySubstring(start stopstart)Trim() + ;
            weather = weatherSubstring()Trim();

  start =
            wea_cityIndexOf(<td>wea_cityLength);
            stop = wea_cityIndexOf(start) + ;
            weather = wea_citySubstring(start stopstart);
            weather = weatherSubstring(stopstart)Trim();
            weather = weatherReplace( );

  start = wea_cityIndexOf(<fontcolor=#><b>
            wea_cityLength);
            stop = wea_cityIndexOf(</b></font>start);
            wea = wea_citySubstring(startstopstart);
            wea = weaSubstring(weaLength) + kbrk;
            wea = weaReplace( );
            wea = weaReplace(> k);
            wea = weaReplace(< k);
            wea = weaReplace(kbrk k);
            string [] wall = null;
            char[] seperator = {k};
            wall = weaSplit(seperator);

  /**///////////////////////////////////////
            l_cityText = [城市] + city;
            l_weaText = [天氣] + weather;
            l_skyText = [溫度] + weather;
            /**////////
            l_dateText = wall[];
            l_wText = wall[];
            l_wText = wall[];
            l_wText = wall[];
            }

  public string weather_city(string city)
            {
            string temp = null;
            try
            {
            string strURL =
            bin/figureWeather/searchcgi;
            HttpWebRequest request;
            request = (HttpWebRequest)WebRequestCreate(strURL);
            requestMethod=POST; //Post請求方式
            requestContentType=application/xwwwformurlencoded; //內容類型
            string paraUrlCoded = SystemWebHttpUtilityUrlEncode(city);
            //參數經過URL編碼
            paraUrlCoded = paraUrlCoded + = +
            SystemWebHttpUtilityUrlEncode(city
            SystemTextEncodingGetEncoding(GB));
            byte[] payload;
            payload =
            SystemTextEncodingGetEncoding(GB)GetBytes(paraUrlCoded);
            //將URL編碼後的字符串轉化為字節
            requestContentLength = payloadLength; //設置請求的ContentLength
            Stream writer = requestGetRequestStream(); //獲得請求流
            writerWrite(payloadpayloadLength); //將請求參數寫入流
            writerClose(); //關閉請求流
            HttpWebResponse response;
            response = (HttpWebResponse)requestGetResponse(); //獲得響應流
            Stream s;
            s = responseGetResponseStream();
            StreamReader objReader = new
            StreamReader(sSystemTextEncodingGetEncoding(GB));
            string HTML = ;
            string sLine = ;
            int i = ;
            while (sLine!=null)
            {
            i++;
            sLine = objReaderReadLine();
            if (sLine!=null)
            HTML += sLine;
            }
            HTML = HTMLReplace(&lt;<);
            HTML = HTMLReplace(&gt;>);
            int startstop;
            start = HTMLIndexOf(<img
            src=HTMLLength);
            stop = HTMLIndexOf(<td
            background=start);
            temp = HTMLSubstring(start stop start);
            }
            catch (Exception x)
            {
            }
            return temp;
            }
            }


From:http://tw.wingwit.com/Article/program/net/201311/11568.html
    推薦文章
    Copyright © 2005-2022 電腦知識網 Computer Knowledge   All rights reserved.