Weather forecast webservice code

Source: Internet
Author: User

<% @ WebService Language = "c #" Class = "WeatherWS. GetChinaWeather" %>
Using System;
Using System. Collections;
Using System. ComponentModel;
Using System. Data;
Using System. Diagnostics;
Using System. Net;
Using System. Text;
Using System. IO;
Using System. Web;
Using System. Web. Services;

Namespace WeatherWS
{
/// <Summary>
/// Summary of GetChinaWeather.
/// </Summary>
[WebService (Namespace = "http://flying.redv.com/monster")]
Public class GetChinaWeather: System. Web. Services. WebService
{
Public GetChinaWeather ()
{
// CODEGEN: This call is required by the ASP. NET Web service designer.
InitializeComponent ();
}

# Code generated by the region component designer

// Required by the Web Service designer
Private IContainer components = null;

/// <Summary>
/// The designer supports the required methods-do not use the code editor to modify
/// Content of this method.
/// </Summary>
Private void InitializeComponent ()
{
}

/// <Summary>
/// Clear all resources in use.
/// </Summary>
Protected override void Dispose (bool disposing)
{
If (disposing & components! = Null)
{
Components. Dispose ();
}
Base. Dispose (disposing );
}

# Endregion

[WebMethod (Description = "Weather Forecasting Service for cities (counties) in China, which can take a string of parameters (optional query method: · domestic cities (counties) full name · pinyin abbreviation · Telephone area code · zip code. If you want to query the weather conditions in Xuzhou, enter 'xuzhou 'or 'xization' as the parameter) ")]
Public DataSet getWeather (string strCity)
{
DataSet dsWeather = new DataSet ();
Try
{
Const int maxDay = 5;
String [] time = new string [maxDay]; // storage date, counted from today
String [] weather = new string [maxDay]; // Save the weather data
String [] max = new string [maxDay]; // stores the maximum temperature data.
String [] min = new string [maxDay]; // Save the minimum temperature data
String [] wind = new string [maxDay]; // save wind direction data

// Send a post request to the index. jsp page to obtain City Data
Uri uri = new Uri ("http://www.weathercn.com/forecastn/forcast/index.jsp? Searchname = "+ System. Web. HttpUtility. UrlEncode (strCity, System. Text. Encoding. GetEncoding (" GB2312 ")));
WebRequest wreq = WebRequest. Create (uri );

HttpWebResponse wresp = (HttpWebResponse) wreq. GetResponse ();

String HTML = "";

Stream s = wresp. GetResponseStream ();

StreamReader objReader = new StreamReader (s, System. Text. Encoding. Default );
HTML = objReader. ReadToEnd ();
If (HTML = null | HTML = "")
Return dsWeather;

HTML = HTML. ToLower (); // convert all to lower case
If (HTML = null | HTML = "")
Return dsWeather;
Int head, tail, I;
// If City data is not found, an empty dataset is returned.
Head = HTML. IndexOf ("query result:", 0 );
Head = HTML. IndexOf ("station_name =", head );
If (head =-1)
{
Return dsWeather;
}
Head = HTML. IndexOf ("station_name =", head + 1 );
Tail = HTML. IndexOf ("'", head );
String strCityData = HTML. Substring (head, tail-head); // obtain City Data
String href = "http://www.weathercn.com/forecastn/forcast/forecastDetail.jsp? "+ StrCityData;
// Query weather conditions based on City Data

Wreq = WebRequest. Create (href );
Wresp = (HttpWebResponse) wreq. GetResponse ();

HTML = "";
S = wresp. GetResponseStream ();

ObjReader = new StreamReader (s, System. Text. Encoding. Default );
HTML = objReader. ReadToEnd ();
If (HTML = null | HTML = "")
Return dsWeather;
HTML = HTML. ToLower (); // convert all to lower case

DateTime dtNow = new DateTime ();
DtNow = DateTime. Today; // obtain the current system date
DtNow = dtNow. Subtract (TimeSpan. Parse ("1 "));
For (I = 0; I <maxDay; I ++)
{
DtNow = dtNow. Add (TimeSpan. Parse ("1 "));
Time [I] = dtNow. tow.datestring (); // Date data
}

// Obtain weather data, total maxDay data
String date = DateTime. Now. Year. ToString () + "Year" + DateTime. Now. Month. ToString () + "Month"; // current Year
Head = HTML. IndexOf (date, 0 );
Head = HTML. IndexOf ("<tr>", head );
For (I = 0; I <maxDay; I ++)
{
Head = HTML. IndexOf ("<td", head );
Head = HTML. IndexOf ("Head = HTML. IndexOf ("/", head );
Head = HTML. IndexOf ("/", head + 1 );
Tail = HTML. IndexOf ("_", head );
Weather [I] = HTML. Substring (head + 1, tail-head-1 );
Head = HTML. IndexOf ("</td>", head );
}

// Obtain the temperature data of the last maxDay, including the maximum temperature and the minimum temperature.
For (I = 0; I <maxDay; I ++)
{
Head = HTML. IndexOf ("max", head );
Head = HTML. IndexOf (">", head );
Tail = HTML. IndexOf ("<", head );
Max [I] = HTML. Substring (head + 1, tail-head-1); // maximum temperature

Head = HTML. IndexOf ("min", head );
Head = HTML. IndexOf (">", head );
Tail = HTML. IndexOf ("<", head );
Min [I] = HTML. Substring (head + 1, tail-head-1); // minimum temperature
}

// Wind direction data for the last maxDay
Head = HTML. IndexOf ("<tr", head );
For (I = 0; I <maxDay; I ++)
{
Head = HTML. IndexOf ("class", head );
Head = HTML. IndexOf (">", head );
Tail = HTML. IndexOf ("<", head );
Wind [I] = HTML. Substring (head + 1, tail-head-1); // wind direction data
}

// Fill the data in DataSet
DataTable dtWeather = new DataTable ();
DtWeather. Columns. Add ("date ");
DtWeather. Columns. Add ("Weather ");
DtWeather. Columns. Add ("maximum temperature ");
DtWeather. Columns. Add ("minimum temperature ");
DtWeather. Columns. Add ("wind direction ");
For (I = 0; I <maxDay; I ++)
{
DataRow drWeather = dtWeather. NewRow ();
DrWeather ["date"] = time [I];
DrWeather ["weather"] = weather [I];
DrWeather ["maximum temperature"] = max [I];
DrWeather ["minimum temperature"] = min [I];
DrWeather ["wind direction"] = wind [I];
DtWeather. Rows. Add (drWeather );
}
DsWeather. Tables. Add (dtWeather );
DsWeather. AcceptChanges ();

Return dsWeather;

}
Catch (Exception e)
{
// DO Something
Return dsWeather;
}
}
}
}

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.