<%@ 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>
/// GetChinaWeather 的摘要说明。
/// </summary>
[WebService(Namespace="http://flying.redv.com/monster")]
public class GetChinaWeather : System.Web.Services.WebService
{
public GetChinaWeather()
{
?? //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 DataSet getWeather(string strCity)
{
?? DataSet dsWeather = new DataSet();
?? 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.Default);
??? 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.Default);
??? 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+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);
???? 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);
???? 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.Tables.Add(dtWeather);
??? dsWeather.AcceptChanges();
??? return dsWeather;
?? } ?? catch(Exception e) ?? { ??? //DO Something ??? return dsWeather; ?? } } } }