查询天气预报的Webservice接口程序源代码
2009-05-31 17:50
351 查看
前一段时间在做毕设的时候需要做一个天气预报服务接口,发现很多网友也在找这方面的资料,现将我我写这个web服务源代码公布如下,希望大家共同学习,共同进步.
<%@ 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);
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.Tables.Add(dtWeather);
dsWeather.AcceptChanges();
return dsWeather;
}
catch(Exception e)
{
//DO Something
return dsWeather;
}
}
}
}
<%@ 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);
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.Tables.Add(dtWeather);
dsWeather.AcceptChanges();
return dsWeather;
}
catch(Exception e)
{
//DO Something
return dsWeather;
}
}
}
}
相关文章推荐
- 查询天气预报的Webservice接口程序源代码
- 查询天气预报的Webservice接口程序源代码
- 查询天气预报的Webservice接口程序源代码
- Delphi 编写调用WebService接口的小程序(天气预报)
- webservice 的应用端(调用第一个程序暴露出来的接口)
- wsdl4j解析webservice的wsdl-根据webservice地址查询接口方法名称
- 手机归属地免费查询接口,api,wsdl,webservice
- Java调用WEBSERVICE接口实现天气预报和获得国内手机号码归属地省份、地区和手机卡类型信息
- webservice实际应用之调用外部系统提供的服务端地址,写java的webservice客户端程序并通过接口给外部系统传数据
- webservice 天气预报的接口
- android中webService接口调用示例:查询电话号码所在地
- 中文电码查询接口 标准中文电码查询程序结果
- 全网、全国最好用的基于经纬度的天气查询、天气预报接口
- 百度提供天气预报查询接口API
- 免费的天气预报webservice接口
- 免费的webservice接口(天气预报/IP查询/股票查询/手机归属地等)
- 写了C# ASP.NET WebService的XML解析网站接口程序收了200元辛苦费【加入软件项目源码交易群的好处】
- 人脸识别api接口demo,小程序人脸识别接口开发源代码
- 手机归属地免费查询接口,api,wsdl,webservice
- 免费的天气预报webservice接口