|
Posted on 2006-10-12 13:02 东人EP 阅读(1736) 评论(0) 编辑 收藏 引用 所属分类: .NET
1
//
天气预报源代码
2
3
using
System;
4
using
System.Collections;
5
using
System.ComponentModel;
6
using
System.Data;
7
using
System.Diagnostics;
8
using
System.Net;
9
using
System.Text;
10
using
System.IO;
11
using
System.Web;
12
using
System.Web.Services;
13
14
namespace
WeatherWS
15
{
16
/**/
///
<summary>
17
///
getCHWeather 的摘要说明。
18
///
</summary>
19
[WebService(Namespace
=
"
http://flying.redv.com/monster
"
)]
20
public
class
getCHWeather : System.Web.Services.WebService
21
{
22
public
getCHWeather()
23
{
24
//
CODEGEN: 该调用是 ASP.NET Web 服务设计器所必需的
25
InitializeComponent();
26
}
27
28
组件设计器生成的代码
#region
组件设计器生成的代码
29
30
//
Web 服务设计器所必需的
31
private
IContainer components
=
null
;
32
33
/**/
///
<summary>
34
///
设计器支持所需的方法 - 不要使用代码编辑器修改
35
///
此方法的内容。
36
///
</summary>
37
private
void
InitializeComponent()
38
{
39
}
40
41
/**/
///
<summary>
42
///
清理所有正在使用的资源。
43
///
</summary>
44
protected
override
void
Dispose(
bool
disposing )
45
{
46
if
(disposing
&&
components
!=
null
)
47
{
48
components.Dispose();
49
}
50
base
.Dispose(disposing);
51
}
52
53
#endregion
54
[WebMethod(Description
=
"
中国各城市(县)天气预报获取服务,可接受一字符串参数(可选的查询方式:·国内城市(县)全名·字首拼音缩写·电话区号·邮政编码,如查询徐州的天气情况可输入'徐州'或'xz'作为参数)
"
)]
55
public
weatherDataClass getWeather(
string
strCity)
56
{
57
weatherDataClass _dsWeather
=
new
weatherDataClass();
58
try
59
{
60
const
int
maxDay
=
5
;
61
string
[]time
=
new
string
[maxDay];
//
存储日期,从今天开始算起
62
string
[]weather
=
new
string
[maxDay];
//
保存天气情况数据
63
string
[]max
=
new
string
[maxDay];
//
保存最高温度数据
64
string
[]min
=
new
string
[maxDay];
//
保存最低温度数据
65
string
[]wind
=
new
string
[maxDay];
//
保存风向数据
66
67
//
发送一个post请求到index.jsp页面以获取城市数据
68
Uri uri
=
new
Uri(
"
http://www.weathercn.com/forecastn/forcast/index.jsp?searchname=
"
+
System.Web.HttpUtility.UrlEncode(strCity,System.Text.Encoding.GetEncoding(
"
GB2312
"
)));
69
WebRequest wreq
=
WebRequest.Create(uri);
70
71
HttpWebResponse wresp
=
(HttpWebResponse)wreq.GetResponse();
72
73
string
HTML
=
""
;
74
75
Stream s
=
wresp.GetResponseStream();
76
77
StreamReader objReader
=
new
StreamReader(s,System.Text.Encoding.GetEncoding(
"
GB2312
"
));
78
HTML
=
objReader.ReadToEnd();
79
if
(HTML
==
null
||
HTML
==
""
)
80
return
_dsWeather;
81
82
HTML
=
HTML.ToLower();
//
全部转换为小写
83
if
(HTML
==
null
||
HTML
==
""
)
84
return
_dsWeather;
85
int
head,tail,i;
86
//
查找城市数据 如果没有找到 则返回一个空的dataset
87
head
=
HTML.IndexOf(
"
查询结果:
"
,
0
);
88
head
=
HTML.IndexOf(
"
station_name=
"
,head);
89
if
(head
==-
1
)
90
{
91
return
_dsWeather;
92
}
93
head
=
HTML.IndexOf(
"
station_name=
"
,head
+
1
);
94
tail
=
HTML.IndexOf(
"
'
"
,head);
95
string
strCityData
=
HTML.Substring(head,tail
-
head);
//
城市数据获取
96
string
href
=
"
http://www.weathercn.com/forecastn/forcast/forecastDetail.jsp?
"
+
strCityData;
97
//
根据城市数据去查询天气情况
98
99
wreq
=
WebRequest.Create(href);
100
wresp
=
(HttpWebResponse)wreq.GetResponse();
101
102
HTML
=
""
;
103
s
=
wresp.GetResponseStream();
104
105
objReader
=
new
StreamReader(s,System.Text.Encoding.GetEncoding(
"
GB2312
"
));
106
HTML
=
objReader.ReadToEnd();
107
if
(HTML
==
null
||
HTML
==
""
)
108
return
_dsWeather;
109
HTML
=
HTML.ToLower();
//
全部转换为小写
110
111
DateTime dtNow
=
new
DateTime();
112
dtNow
=
DateTime.Today;
//
获取系统当前日期
113
dtNow
=
dtNow.Subtract(TimeSpan.Parse(
"
1
"
));
114
for
(i
=
0
;i
<
maxDay;i
++
)
115
{
116
dtNow
=
dtNow.Add(TimeSpan.Parse(
"
1
"
));
117
time
=
dtNow.ToShortDateString();
//
日期数据
118
}
119
120
//
获取天气情况数据,总共maxDay天的数据
121
String date
=
DateTime.Now.Year.ToString()
+
"
年
"
+
DateTime.Now.Month.ToString()
+
"
月
"
;
//
当前年月
122
head
=
HTML.IndexOf(date,
0
);
123
head
=
HTML.IndexOf(
"
<tr>
"
,head);
124
for
(i
=
0
;i
<
maxDay;i
++
)
125
{
126
head
=
HTML.IndexOf(
"
<td
"
,head);
127
head
=
HTML.IndexOf(
"
<img
"
,head);
128
head
=
HTML.IndexOf(
"
/
"
,head);
129
head
=
HTML.IndexOf(
"
/
"
,head
+
1
);
130
tail
=
HTML.IndexOf(
"
_
"
,head);
131
weather
=
HTML.Substring(head
+
1
,tail
-
head
-
1
);
132
head
=
HTML.IndexOf(
"
</td>
"
,head);
133
}
134
135
//
获取近maxDay天温度数据,包括最高温度和最低温度
136
for
(i
=
0
;i
<
maxDay;i
++
)
137
{
138
head
=
HTML.IndexOf(
"
max
"
,head);
139
head
=
HTML.IndexOf(
"
>
"
,head);
140
tail
=
HTML.IndexOf(
"
<
"
,head);
141
max
=
HTML.Substring(head
+
1
,tail
-
head
-
1
);
//
最高温度
142
143
head
=
HTML.IndexOf(
"
min
"
,head);
144
head
=
HTML.IndexOf(
"
>
"
,head);
145
tail
=
HTML.IndexOf(
"
<
"
,head);
146
min
=
HTML.Substring(head
+
1
,tail
-
head
-
1
);
//
最低温度
147
}
148
149
//
最近maxDay天的风向数据
150
head
=
HTML.IndexOf(
"
<tr
"
,head);
151
for
(i
=
0
;i
<
maxDay;i
++
)
152
{
153
head
=
HTML.IndexOf(
"
class
"
,head);
154
head
=
HTML.IndexOf(
"
>
"
,head);
155
tail
=
HTML.IndexOf(
"
<
"
,head);
156
wind
=
HTML.Substring(head
+
1
,tail
-
head
-
1
);
//
风向数据
157
}
158
159
//
将数据填充到DataSet中去
160
DataTable dtWeather
=
new
DataTable();
161
dtWeather.Columns.Add(
"
日期
"
);
162
dtWeather.Columns.Add(
"
天气
"
);
163
dtWeather.Columns.Add(
"
最高温度
"
);
164
dtWeather.Columns.Add(
"
最低温度
"
);
165
dtWeather.Columns.Add(
"
风力风向
"
);
166
for
(i
=
0
;i
<
maxDay;i
++
)
167
{
168
DataRow drWeather
=
dtWeather.NewRow();
169
drWeather[
"
日期
"
]
=
time;
170
drWeather[
"
天气
"
]
=
weather;
171
drWeather[
"
最高温度
"
]
=
max;
172
drWeather[
"
最低温度
"
]
=
min;
173
drWeather[
"
风力风向
"
]
=
wind;
174
dtWeather.Rows.Add(drWeather);
175
}
176
_dsWeather.dsWeather
=
new
DataSet(
"
weather
"
);
177
_dsWeather.dsWeather.Tables.Add(dtWeather);
178
_dsWeather.dsWeather.AcceptChanges();
179
//
开始获取其它数据
180
//
城市具体位置
181
/**/
/*
182
head = HTML.IndexOf("120小时天气预报",0);
183
head = HTML.IndexOf("<td",head);
184
head = HTML.IndexOf(">",head);
185
tail = HTML.IndexOf("<",head);
186
//_dsWeather.cityDetail = HTML.Substring(head+1,tail-head-1);
187
head = HTML.IndexOf(">",tail);
188
tail = HTML.IndexOf("<",head);
189
//_dsWeather.cityDetail += HTML.Substring(head+1,tail-head-1);
190
//电话区号以及邮政编码
191
head = HTML.IndexOf("<b>",tail);
192
tail = HTML.IndexOf("</b>",head);
193
//_dsWeather.tel = HTML.Substring(head+3,tail-head-3);
194
//邮编
195
head = HTML.IndexOf("<b>",tail);
196
tail = HTML.IndexOf("</b>",head);
197
//_dsWeather.zip = HTML.Substring(head+3,tail-head-3);
198
*/
199
//
城市简介:
200
head
=
HTML.IndexOf(
"
城市简介:
"
,
0
);
201
tail
=
HTML.IndexOf(
"
</textarea>
"
,head);
202
_dsWeather.cityDes
=
HTML.Substring(head
+
5
,tail
-
head
-
5
);
203
204
return
_dsWeather;
205
206
}
207
catch
(Exception e)
208
{
209
//
DO Something
210
return
_dsWeather;
211
}
212
}
213
//
该类用于保存天气数据
214
public
class
weatherDataClass
215
{
216
public
weatherDataClass()
217
{
218
tel
=
zip
=
cityDetail
=
cityDes
=
liveDes
=
deaDes
=
""
;
219
}
220
public
DataSet dsWeather;
//
天气数据
221
public
string
tel;
//
电话区号
222
public
string
zip;
//
邮政编码
223
public
string
cityDetail;
//
城市具体位置
224
public
string
cityDes;
//
城市介绍
225
public
string
liveDes;
//
生活指数
226
public
string
deaDes;
//
疾病指数
227
}
228
}
229
}
|