Traditional Culture Encyclopedia - Weather inquiry - ASP.NET realizes weather forecast.

ASP.NET realizes weather forecast.

Use the system;

Use the system. Data;

Use the system. Configuration;

Use the system. Web

Use the system. Network security;

Use the system. Web . UI

Use the system. Web . UI.WebControls

Use the system. web . ui . web controls . web parts;

Use the system. Web . UI.HtmlControls

Use the system. Data . SqlClient

Use System.Net;

Use the system. Io;

Use the system. Collection;

///& lt; Summary & gt

///A brief description of the weather

///& lt; /summary & gt;

Public weather

{

Public weather ()

{

//

// TODO: Add constructor logic here.

//

}

Public static string ConvertCodeByCity (string city)

{

String code = "";

Switch (city)

{

Case "Beijing":

code = " 1 10 100 ";

Break;

Default value:

Break;

}

Return code;

}

Public static array list GetWeather (string code)

{

/*

[0] "Beijing" string

[1] "Thunderstorm" string

[2] "9℃" string

[3] "29℃" string

[4] "Below Level 3" string

*/

String html =

attempt

{

HttpWebRequest request = (HttpWebRequest)WebRequest. create("/iframe/weather/"+code+" _ w . html ");

Request. Method = " Get

//request. Timeout =1;

Request. content type = " application/x-www-form-urlencoded ";

WebResponse response = request. GetResponse();

Stream s = response. GetResponseStream();

StreamReader sr = new StreamReader(s, system. text . encoding . get encoding(" GB 23 12 ");

html = Sr . ReadToEnd();

South close ();

Sr . Close();

}

Catch (exception error)

{

Thrownnewexception ("Error accessing address ~ ~ ~);

}

int count = html。 Length;

int starIndex = html。 index of(" & lt; Table ",0, count);

int endIndex = html。 index of(" & lt; /table >,starIndex,count-star index);

html = html。 Substring(starIndex,end index-starIndex+8);

//Get the city

int cityStartIndex = html。 index of(" & lt; b >; ,0,html。 Length);

int cityEndIndex = html。 index of(" & lt; /b & gt; ,0,html。 Length);

String City = html. Substring(cityStartIndex + 3,city endindex-city startindex-3);

//Get the weather

int weatherStartIndex = html。 index of(" & lt; b >; ,city endindex);

int weatherEndIndex = html。 index of(" & lt; /b & gt; ,weatherStartIndex);

The string Weather = html. Substring(weatherStartIndex + 3,weather endindex-weather startindex-3);

//Get the temperature

int temperatureStartIndex = html。 index of(" & lt; b”,weather endindex);

int temperatureEndIndex = html。 index of(" & lt; /b & gt; ,weather endindex+3);

String temperature = html. substring(temperature startindex+2 1,temperature endindex-temperature startindex-2 1);

Int int 1 = temperature. IndexOf("℃",0);

Int int2 = temperature. IndexOf("~ ",0);

Int int3 = temperature. IndexOf("℃",int 2);

String MinTemperature = temperature. Substring (int2+1, int 3-int 2);

String MaxTemperature = temperature. Substring (0, int 2-int1+2);

//Obtain wind power generation.

int wind force startindex = html . index of(" wind:",temperature endindex);

int windforceEndIndex = html。 index of(" & lt; br >; ,windforceStartIndex);

string Windforce = html。 substring(windforceStartIndex+3,windforcendex-windforceStartIndex-3);

If (the wind. Contains ("less than")&; & amp(! Wind power. Contains(" equal to "))////Judge whether the wind force contains the words "less than" or "less than or equal to" and replace it with 2-

{

//Windforce = wind force. Replace ("less than", "2-);

string strWindforce = Windforce。 Substring (2, Windforce. Length-3);

int minWindforce = Int32。 parse(strWindforce)- 1;

Wind power = wind power. Replace ("less than", minWindforce. ToString()+"-";

}

Else if (Windforce。 Contain ("less than or equal to"))

{

string strWindforce = Windforce。 Substring (4, Windforce. Length-5);

int minWindforce = Int32。 parse(strWindforce)- 1;

Wind power = wind power. Replace ("less than or equal to", minWindforce. ToString()+"-";

}

ArrayList al = new ArrayList();

Al. Add (city);

Al. Add (weather);

Al. Add (minimum temperature);

Al. add(MaxTemperature);

Al. add(wind force);

Return to al;

}

}