Read SINA weather forecast to WAP page with C # (ii)

Source: Internet
Author: User
Sina| Weather Forecast | page

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)
  {
   // Place user code here to initialize page
   if (! Page.IsPostBack)
   {
    string city = "Shenzhen";
    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 = "[Cities]:" + City;
L_wea. Text = "[Weather]:" + weather1;
L_sky. Text = "[Temperature]:" + weather2;
///////
L_date. Text = wall[0];
L_w1. Text = wall[1];
L_w2. Text = wall[2];
L_w3. Text = wall[3];
}
}

Code generated #region the Web forms Designer
Override protected void OnInit (EventArgs e)
{
//
CodeGen: This call is required for the ASP.net Web forms Designer.
//
InitializeComponent ();
Base. OnInit (e);
}

<summary>
Designer supports required methods-do not use the Code editor to modify
The contents of this method.
</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 = "[Cities]:" + City;
L_wea. Text = "[Weather]:" + weather1;
L_sky. Text = "[Temperature]:" + 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 method
Request. Contenttype= "application/x-www-form-urlencoded"; Content Type
String paraurlcoded = System.Web.HttpUtility.UrlEncode ("city"); Parameters are encoded by URL
paraurlcoded = paraurlcoded + "=" + System.Web.HttpUtility.UrlEncode (city, System.Text.Encoding.GetEncoding ("GB2312") );
Byte[] payload;
Payload = System.Text.Encoding.GetEncoding ("GB2312"). GetBytes (paraurlcoded); Converts a URL-encoded string to a byte
Request. ContentLength = payload. Length; Set the contentlength of the request
Stream writer = Request. GetRequestStream (); Get Request Flow
Writer. Write (payload,0,payload. Length); Write request parameters to stream
Writer. Close (); Close Request Flow
HttpWebResponse response;
Response = (HttpWebResponse) request. GetResponse (); Get response Flow
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 ("<", "<");
html = HTML. Replace (">", ">");
int start,stop;
Start = HTML. IndexOf ("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;
}
}




Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.