Use C # To read the sina weather forecast to the wap page (2)

Source: Internet
Author: User

 

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 the 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;
Weatsp2 = wea_city.Substring (start, stop-start );
Weatsp2 = weather2.Substring (stop-start-42, 40). Trim ();
Weatsp2 = weather2.Replace ("","");

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 ("","");
Wea = wea. Replace (">", "k ");
Wea = wea. Replace ("<", "k ");
Wea = wea. Replace ("kbrk", "k ");
String [] wall = null;
Char [] seperator = {k };
Wall = wea. Split (seperator );

//////////////////////////////////////
Rochelle city.text = "[city]:" + city;
Rochelle wea.text = "[weather]:" + weather1;
Rochelle sky.text = "[temperature]:" + weather2;
///////
Rochelle date.text = wall [0];
Rochelle w1.text = wall [1];
Rochelle w2.text = wall [2];
Rochelle w3.text = wall [3];
}
}

# Code generated by region Web Form Designer
Override protected void OnInit (EventArgs e)
{
//
// CODEGEN: This call is required by the ASP. NET Web form designer.
//
InitializeComponent ();
Base. OnInit (e );
}

/// <Summary>
/// The designer supports the required methods-do not use the code editor to modify
/// Content 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 commandateclick (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;
Weatsp2 = wea_city.Substring (start, stop-start );
Weatsp2 = weather2.Substring (stop-start-42, 40). Trim ();
Weatsp2 = weather2.Replace ("","");

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 ("","");
Wea = wea. Replace (">", "k ");
Wea = wea. Replace ("<", "k ");
Wea = wea. Replace ("kbrk", "k ");
String [] wall = null;
Char [] seperator = {k };
Wall = wea. Split (seperator );

//////////////////////////////////////
Rochelle city.text = "[city]:" + city;
Rochelle wea.text = "[weather]:" + weather1;
Rochelle sky.text = "[temperature]:" + weather2;
///////
Rochelle date.text = wall [0];
Rochelle w1.text = wall [1];
Rochelle w2.text = wall [2];
Rochelle 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"); // The parameter is URL encoded.
ParaUrlCoded = paraUrlCoded + "=" + System. Web. HttpUtility. UrlEncode (city, System. Text. Encoding. GetEncoding ("GB2312 "));
Byte [] payload;
Payload = System. Text. Encoding. GetEncoding ("GB2312"). GetBytes (paraUrlCoded); // convert the URL-encoded string into bytes.
Request. ContentLength

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.