国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發 > 綜合 > 正文

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

2024-07-21 02:17:48
字體:
來源:轉載
供稿:網友
菜鳥學堂:
 

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


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 南投县| 台山市| 贵定县| 怀来县| 仁化县| 吴堡县| 华安县| 井陉县| 涞源县| 晋中市| 老河口市| 登封市| 友谊县| 乾安县| 姚安县| 乐清市| 高尔夫| 郧西县| 琼结县| 探索| 奈曼旗| 获嘉县| 城口县| 清河县| 稷山县| 平陆县| SHOW| 贵德县| 鲁甸县| 茂名市| 当阳市| 葫芦岛市| 克东县| 安国市| 炉霍县| 辽阳县| 南岸区| 民权县| 神农架林区| 万山特区| 理塘县|