完整的天气预报web服务源码

来源:互联网 发布:百度推广账户优化公司 编辑:程序博客网 时间:2024/05/21 04:23

完整的天气预报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("120小时天气预报",0);
head = HTML.IndexOf("<td",head);
head = HTML.IndexOf(">",head);
tail = HTML.IndexOf("<",head);
//_dsWeather.cityDetail = HTML.Substring(head+1,tail-head-1);
head = HTML.IndexOf(">",tail);
tail = HTML.IndexOf("<",head);
//_dsWeather.cityDetail += HTML.Substring(head+1,tail-head-1);
//电话区号以及邮政编码
head = HTML.IndexOf("<b>",tail);
tail = HTML.IndexOf("</b>",head);
//_dsWeather.tel = HTML.Substring(head+3,tail-head-3);
//邮编
head = HTML.IndexOf("<b>",tail);
tail = HTML.IndexOf("</b>",head);
//_dsWeather.zip = HTML.Substring(head+3,tail-head-3);
*/
//城市简介:
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;//疾病指数
}
}
原创粉丝点击