完整的天氣預報web服務源碼
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>
/// getCHWeather 的摘要說明。
/// </summary>
[WebService(Namespace=" http://flying.redv.com/monster")]
public class getCHWeather : System.Web.Services.WebService
{
public getCHWeather()
{
//CODEGEN: 該調用是 ASP.NET Web 服務設計器所必需的
InitializeComponent();
} #region 元件設計器生成的代碼 //Web 服務設計器所必需的
private IContainer components = null; /// <summary>
/// 設計器支援所需的方法 - 不要使用代碼編輯器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
} /// <summary>
/// 清理所有正在使用的資源。
/// </summary>
protected override void Dispose( bool disposing )
{
if(disposing && components != null)
{
components.Dispose();
}
base.Dispose(disposing);
} #endregion
[WebMethod(Description="中國各城市(縣)天氣預報擷取服務,可接受一字元串參數(可選的查詢方式:·國内城市(縣)全名·字首拼音縮寫·電話區号·郵政編碼,如查詢徐州的天氣情況可輸入'徐州'或'xz'作為參數)")]
public weatherDataClass getWeather(string strCity)
{
weatherDataClass _dsWeather = new weatherDataClass();
try
{
const int maxDay=5;
string []time = new string[maxDay];//存儲日期,從今天開始算起
string []weather = new string[maxDay];//儲存天氣情況資料
string []max = new string[maxDay];//儲存最高溫度資料
string []min = new string[maxDay];//儲存最低溫度資料
string []wind = new string[maxDay];//儲存風向資料 //發送一個post請求到index.jsp頁面以擷取城市資料
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.GetEncoding("GB2312"));
HTML=objReader.ReadToEnd();
if(HTML==null||HTML=="")
return _dsWeather; HTML = HTML.ToLower();//全部轉換為小寫
if(HTML==null||HTML=="")
return _dsWeather;
int head,tail,i;
//查找城市資料 如果沒有找到 則傳回一個空的dataset
head = HTML.IndexOf("查詢結果:",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);//城市資料擷取
string href = " http://www.weathercn.com/forecastn/forcast/forecastDetail.jsp?"+strCityData;
//根據城市資料去查詢天氣情況 wreq=WebRequest.Create(href);
wresp=(HttpWebResponse)wreq.GetResponse();
HTML ="";
s=wresp.GetResponseStream();
objReader = new StreamReader(s,System.Text.Encoding.GetEncoding("GB2312"));
HTML=objReader.ReadToEnd();
if(HTML==null||HTML=="")
return _dsWeather;
HTML = HTML.ToLower();//全部轉換為小寫 DateTime dtNow = new DateTime();
dtNow = DateTime.Today;//擷取系統目前日期
dtNow = dtNow.Subtract(TimeSpan.Parse("1"));
for(i=0;i<maxDay;i++)
{
dtNow = dtNow.Add(TimeSpan.Parse("1"));
time[i] = dtNow.ToShortDateString();//日期資料
} //擷取天氣情況資料,總共maxDay天的資料
String date = DateTime.Now.Year.ToString()+"年"+DateTime.Now.Month.ToString()+"月";//目前年月
head = HTML.IndexOf(date,0);
head = HTML.IndexOf("<tr>",head);
for(i=0;i<maxDay;i++)
{
head = HTML.IndexOf("<td",head);
head = HTML.IndexOf("<img",head);
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);
} //擷取近maxDay天溫度資料,包括最高溫度和最低溫度
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);//最高溫度 head = HTML.IndexOf("min",head);
head = HTML.IndexOf(">",head);
tail = HTML.IndexOf("<",head);
min[i] = HTML.Substring(head+1,tail-head-1);//最低溫度
} //最近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);//風向資料
} //将資料填充到DataSet中去
DataTable dtWeather = new DataTable();
dtWeather.Columns.Add("日期");
dtWeather.Columns.Add("天氣");
dtWeather.Columns.Add("最高溫度");
dtWeather.Columns.Add("最低溫度");
dtWeather.Columns.Add("風力風向");
for(i=0;i<maxDay;i++)
{
DataRow drWeather = dtWeather.NewRow();
drWeather["日期"] = time[i];
drWeather["天氣"] = weather[i];
drWeather["最高溫度"] = max[i];
drWeather["最低溫度"] = min[i];
drWeather["風力風向"] = wind[i];
dtWeather.Rows.Add(drWeather);
}
_dsWeather.dsWeather = new DataSet("weather");
_dsWeather.dsWeather.Tables.Add(dtWeather);
_dsWeather.dsWeather.AcceptChanges();
//開始擷取其它資料
//城市具體位置
//城市簡介:
head = HTML.IndexOf("城市簡介:",0);
tail = HTML.IndexOf("</textarea>",head);
_dsWeather.cityDes = HTML.Substring(head+5,tail-head-5); return _dsWeather; }
catch(Exception e)
{
//DO Something
return _dsWeather;
}
}
//該類用于儲存天氣資料
public class weatherDataClass
{
public weatherDataClass()
{
tel = zip = cityDetail = cityDes = liveDes = deaDes ="";
}
public DataSet dsWeather;//天氣資料
public string tel;//電話區号
public string zip;//郵政編碼
public string cityDetail;//城市具體位置
public string cityDes;//城市介紹
public string liveDes;//生活指數
public string deaDes;//疾病指數
}
}
}