自己需要用到天气的WebService的时候,写的一个WebService,提供参考吧.
///
<summary>
/// wsWeather 的摘要说明
/// </summary>
[WebService(Namespace = " http://lespos.org/ ")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
public class wsWeather : System.Web.Services.WebService {
SqlConnection conn = new SqlConnection();
public wsWeather () {
// 如果使用设计的组件,请取消注释以下行
// InitializeComponent();
}
#region Method for getAllDayWeatherInfo // 获取全天天气信息
/// <summary>
/// 获取全天天气信息
/// </summary>
/// <param name="CityCode"> 城市代码 </param>
/// <returns> Json数据 </returns>
[WebMethod]
public string getAllDayWeatherInfo( string CityCode)
{
if ( string.IsNullOrEmpty(CityCode))
{
CityCode = " 101020100 "; // 默认编码上海
}
string strUrl = @" http://www.weather.com.cn/data/cityinfo/ " + CityCode + " .html ";
string tempurl = @" http://www.weather.com.cn/data/list3/city.xml?level=1 ";
string strResult = GetUrltoHtml(tempurl);
return strResult;
}
#endregion
#region Method for getRealTimeWeatherInfo // 获取实时天气信息
/// <summary>
/// 获取实时天气信息
/// </summary>
/// <param name="CityCode"> 城市代码 </param>
/// <returns> Json数据 </returns>
[WebMethod]
public string getRealTimeWeatherInfo( string CityCode)
{
if ( string.IsNullOrEmpty(CityCode))
{
CityCode = " 101020100 "; // 默认编码上海
}
string strUrl = @" http://www.weather.com.cn/data/sk/ " + CityCode + " .html ";
string strResult = GetUrltoHtml(strUrl);
return strResult;
}
#endregion
#region Method for getSevenDaysWeatherInfo // 获取7天内天气信息
/// <summary>
/// 获取7天内天气信息
/// </summary>
/// <param name="CityCode"> 城市代码 </param>
/// <returns> Json数据 </returns>
[WebMethod]
public string getSevenDaysWeatherInfo( string CityCode)
{
if ( string.IsNullOrEmpty(CityCode))
{
CityCode = " 101020100 "; // 默认编码上海
}
string strUrl = @" http://m.weather.com.cn/data/ " + CityCode + " .html ";
string strResult = GetUrltoHtml(strUrl);
return strResult;
}
#endregion
#region Method for GetUrltoHtml // 获取Url请求的内容(Get方式)
/// <summary>
/// 获取Url请求的内容
/// </summary>
/// <param name="Url"></param>
/// <returns></returns>
public string GetUrltoHtml( string Url)
{
try
{
System.Net.WebRequest wReq = System.Net.WebRequest.Create(Url);
System.Net.WebResponse wResp = wReq.GetResponse();
System.IO.Stream respStream = wResp.GetResponseStream();
using (System.IO.StreamReader reader = new System.IO.StreamReader(respStream, Encoding.GetEncoding( " utf-8 ")))
{
return reader.ReadToEnd();
}
}
catch (System.Exception ex)
{
}
return "";
}
#endregion
#region Method for GetUrltoHtml // 获取Url请求的内容(Post方式)
/// <summary>
/// 获取Url请求的内容
/// </summary>
/// <param name="Url"></param>
/// <returns></returns>
public static string OpenReadWithHttps( string URL)
{
try
{
Encoding encoding = Encoding.Default;
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(URL);
request.Method = " post ";
request.Accept = " text/html, application/xhtml+xml, */* ";
request.ContentType = " application/x-www-form-urlencoded ";
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
using (StreamReader reader = new StreamReader(response.GetResponseStream(), System.Text.Encoding.GetEncoding( " utf-8 ")))
{
return reader.ReadToEnd();
}
}
catch (System.Exception ex)
{
}
return "";
}
#endregion
#region Method for TempAddWeather // 添加天气地区信息
/// <summary>
///
/// </summary>
/// <returns></returns>
[WebMethod]
private string TempAddWeather()
{
LESDB.SQLDB clsdb = new LESDB.SQLDB();
try
{
string tempurl = @" http://www.weather.com.cn/data/list3/city.xml?level=1 ";
string strPrivance = GetUrltoHtml(tempurl);
if (!strPrivance.Contains( " html "))
{
string[] PrivanceList = strPrivance.Split( ' , ');
foreach ( string tempPrivance in PrivanceList)
{
string[] tempPrivanceInfo = tempPrivance.Split( ' | ');
string PrivanceCode = tempPrivanceInfo[ 0].Trim(); // 省份编码
string PrivanceName = tempPrivanceInfo[ 1].Trim(); // 省份名称
string tempUrlC = @" http://www.weather.com.cn/data/list3/city " + PrivanceCode + " .xml?level=2 ";
string strCity = GetUrltoHtml(tempUrlC);
if (!strCity.Contains( " html "))
{
string[] CityList = strCity.Split( ' , ');
foreach ( string tempCity in CityList)
{
string[] tempCityInfo = tempCity.Split( ' | ');
string CityCode =tempCityInfo[ 0].Trim() ; // 城市编码
string CityName = tempCityInfo[ 1].Trim(); // 城市名称
string tempUrlA = @" http://www.weather.com.cn/data/list3/city " + CityCode + " .xml?level=3 ";
string strArea = GetUrltoHtml(tempUrlA);
CityCode = " 101 " + CityCode+ " 01 ";
if (!strArea.Contains( " html "))
{
string[] AreaList = strArea.Split( ' , ');
foreach ( string tempArea in AreaList)
{
string[] tempAreaInfo = tempArea.Split( ' | ');
string AreaCode = " 101 " + tempAreaInfo[ 0].Trim(); // 地区编码
string AreaName = tempAreaInfo[ 1].Trim(); // 地区名称
// 添加记录
// 根据门店获取对应城市的代码
clsdb.OpenConn( ref conn);
string sqlstr = " Insert into WeatherInfo values('{0}','{1}','{2}','{3}','{4}','{5}') ";
sqlstr = string.Format(sqlstr, PrivanceCode, PrivanceName, CityCode, CityName, AreaCode, AreaName);
int retInt = clsdb.ExecuteSQl( ref conn, sqlstr);
if (retInt == 0)
return " Failed ";
}
}
}
}
}
}
}
catch (Exception ex)
{
// LogInfo.AddLog("[" + this.GetType().Name + "]-[GetCityCode]-" + ex.Message);
return ex.ToString();
}
finally
{
clsdb.CloseConn( ref conn);
}
return " OK ";
}
#endregion
#region Method for TempWeatherInfo // 检查天气地区信息是否正常
/// <summary>
/// 检查天气地区信息是否正常
/// </summary>
/// <returns></returns>
[WebMethod]
public string TempWeatherInfo()
{
string Result = string.Empty;
LESDB.SQLDB clsdb = new LESDB.SQLDB();
clsdb.OpenConn( ref conn);
string sqlstr = " select AreaCode,AreaName from dbo.WeatherInfo(nolock) where id>=500 and id<=1000 ";
DataTable dt= clsdb.GetDataTable( ref conn, sqlstr);
foreach (DataRow row in dt.Rows)
{
string AreaName = row[ " AreaName "].ToString();
string AreaCode = row[ " AreaCode "].ToString();
string tempUrlA = @" http://m.weather.com.cn/data/ " + AreaCode + " .html ";
string strArea = GetUrltoHtml(tempUrlA);
if (strArea.Contains( " weatherinfo "))
{
Result += " [ " + AreaName + " : " + AreaCode + " ----OK]/n ";
}
else
{
Result += " [ " + AreaName + " : " + AreaCode + " ----Failed]/n ";
}
}
return Result;
}
#endregion
}
/// wsWeather 的摘要说明
/// </summary>
[WebService(Namespace = " http://lespos.org/ ")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
public class wsWeather : System.Web.Services.WebService {
SqlConnection conn = new SqlConnection();
public wsWeather () {
// 如果使用设计的组件,请取消注释以下行
// InitializeComponent();
}
#region Method for getAllDayWeatherInfo // 获取全天天气信息
/// <summary>
/// 获取全天天气信息
/// </summary>
/// <param name="CityCode"> 城市代码 </param>
/// <returns> Json数据 </returns>
[WebMethod]
public string getAllDayWeatherInfo( string CityCode)
{
if ( string.IsNullOrEmpty(CityCode))
{
CityCode = " 101020100 "; // 默认编码上海
}
string strUrl = @" http://www.weather.com.cn/data/cityinfo/ " + CityCode + " .html ";
string tempurl = @" http://www.weather.com.cn/data/list3/city.xml?level=1 ";
string strResult = GetUrltoHtml(tempurl);
return strResult;
}
#endregion
#region Method for getRealTimeWeatherInfo // 获取实时天气信息
/// <summary>
/// 获取实时天气信息
/// </summary>
/// <param name="CityCode"> 城市代码 </param>
/// <returns> Json数据 </returns>
[WebMethod]
public string getRealTimeWeatherInfo( string CityCode)
{
if ( string.IsNullOrEmpty(CityCode))
{
CityCode = " 101020100 "; // 默认编码上海
}
string strUrl = @" http://www.weather.com.cn/data/sk/ " + CityCode + " .html ";
string strResult = GetUrltoHtml(strUrl);
return strResult;
}
#endregion
#region Method for getSevenDaysWeatherInfo // 获取7天内天气信息
/// <summary>
/// 获取7天内天气信息
/// </summary>
/// <param name="CityCode"> 城市代码 </param>
/// <returns> Json数据 </returns>
[WebMethod]
public string getSevenDaysWeatherInfo( string CityCode)
{
if ( string.IsNullOrEmpty(CityCode))
{
CityCode = " 101020100 "; // 默认编码上海
}
string strUrl = @" http://m.weather.com.cn/data/ " + CityCode + " .html ";
string strResult = GetUrltoHtml(strUrl);
return strResult;
}
#endregion
#region Method for GetUrltoHtml // 获取Url请求的内容(Get方式)
/// <summary>
/// 获取Url请求的内容
/// </summary>
/// <param name="Url"></param>
/// <returns></returns>
public string GetUrltoHtml( string Url)
{
try
{
System.Net.WebRequest wReq = System.Net.WebRequest.Create(Url);
System.Net.WebResponse wResp = wReq.GetResponse();
System.IO.Stream respStream = wResp.GetResponseStream();
using (System.IO.StreamReader reader = new System.IO.StreamReader(respStream, Encoding.GetEncoding( " utf-8 ")))
{
return reader.ReadToEnd();
}
}
catch (System.Exception ex)
{
}
return "";
}
#endregion
#region Method for GetUrltoHtml // 获取Url请求的内容(Post方式)
/// <summary>
/// 获取Url请求的内容
/// </summary>
/// <param name="Url"></param>
/// <returns></returns>
public static string OpenReadWithHttps( string URL)
{
try
{
Encoding encoding = Encoding.Default;
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(URL);
request.Method = " post ";
request.Accept = " text/html, application/xhtml+xml, */* ";
request.ContentType = " application/x-www-form-urlencoded ";
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
using (StreamReader reader = new StreamReader(response.GetResponseStream(), System.Text.Encoding.GetEncoding( " utf-8 ")))
{
return reader.ReadToEnd();
}
}
catch (System.Exception ex)
{
}
return "";
}
#endregion
#region Method for TempAddWeather // 添加天气地区信息
/// <summary>
///
/// </summary>
/// <returns></returns>
[WebMethod]
private string TempAddWeather()
{
LESDB.SQLDB clsdb = new LESDB.SQLDB();
try
{
string tempurl = @" http://www.weather.com.cn/data/list3/city.xml?level=1 ";
string strPrivance = GetUrltoHtml(tempurl);
if (!strPrivance.Contains( " html "))
{
string[] PrivanceList = strPrivance.Split( ' , ');
foreach ( string tempPrivance in PrivanceList)
{
string[] tempPrivanceInfo = tempPrivance.Split( ' | ');
string PrivanceCode = tempPrivanceInfo[ 0].Trim(); // 省份编码
string PrivanceName = tempPrivanceInfo[ 1].Trim(); // 省份名称
string tempUrlC = @" http://www.weather.com.cn/data/list3/city " + PrivanceCode + " .xml?level=2 ";
string strCity = GetUrltoHtml(tempUrlC);
if (!strCity.Contains( " html "))
{
string[] CityList = strCity.Split( ' , ');
foreach ( string tempCity in CityList)
{
string[] tempCityInfo = tempCity.Split( ' | ');
string CityCode =tempCityInfo[ 0].Trim() ; // 城市编码
string CityName = tempCityInfo[ 1].Trim(); // 城市名称
string tempUrlA = @" http://www.weather.com.cn/data/list3/city " + CityCode + " .xml?level=3 ";
string strArea = GetUrltoHtml(tempUrlA);
CityCode = " 101 " + CityCode+ " 01 ";
if (!strArea.Contains( " html "))
{
string[] AreaList = strArea.Split( ' , ');
foreach ( string tempArea in AreaList)
{
string[] tempAreaInfo = tempArea.Split( ' | ');
string AreaCode = " 101 " + tempAreaInfo[ 0].Trim(); // 地区编码
string AreaName = tempAreaInfo[ 1].Trim(); // 地区名称
// 添加记录
// 根据门店获取对应城市的代码
clsdb.OpenConn( ref conn);
string sqlstr = " Insert into WeatherInfo values('{0}','{1}','{2}','{3}','{4}','{5}') ";
sqlstr = string.Format(sqlstr, PrivanceCode, PrivanceName, CityCode, CityName, AreaCode, AreaName);
int retInt = clsdb.ExecuteSQl( ref conn, sqlstr);
if (retInt == 0)
return " Failed ";
}
}
}
}
}
}
}
catch (Exception ex)
{
// LogInfo.AddLog("[" + this.GetType().Name + "]-[GetCityCode]-" + ex.Message);
return ex.ToString();
}
finally
{
clsdb.CloseConn( ref conn);
}
return " OK ";
}
#endregion
#region Method for TempWeatherInfo // 检查天气地区信息是否正常
/// <summary>
/// 检查天气地区信息是否正常
/// </summary>
/// <returns></returns>
[WebMethod]
public string TempWeatherInfo()
{
string Result = string.Empty;
LESDB.SQLDB clsdb = new LESDB.SQLDB();
clsdb.OpenConn( ref conn);
string sqlstr = " select AreaCode,AreaName from dbo.WeatherInfo(nolock) where id>=500 and id<=1000 ";
DataTable dt= clsdb.GetDataTable( ref conn, sqlstr);
foreach (DataRow row in dt.Rows)
{
string AreaName = row[ " AreaName "].ToString();
string AreaCode = row[ " AreaCode "].ToString();
string tempUrlA = @" http://m.weather.com.cn/data/ " + AreaCode + " .html ";
string strArea = GetUrltoHtml(tempUrlA);
if (strArea.Contains( " weatherinfo "))
{
Result += " [ " + AreaName + " : " + AreaCode + " ----OK]/n ";
}
else
{
Result += " [ " + AreaName + " : " + AreaCode + " ----Failed]/n ";
}
}
return Result;
}
#endregion
}
最后两个方法 TempAddWeather 和 TempWeatherInfo 是把省份城市地区的编码保存到数据库中,和测试连接是否访问正常的方法。
需要的朋友可以根据自己的实际情况修改下.
这个是根据气象局提供的接口写的,时间匆忙,所以写了点,需要的朋友自己扩展吧.