调用接口,写个webserver,再调用webserver
接口:http://webservice.webxml.com.cn/WebServices/WeatherWS.asmx
写webserver:
[WebMethod(Description = "获取江门天气预报")]
public string[] WeatherOfJiangMen(string citycode)
{
string[] strWeather = new string[32];
WFIT.BLL.WeatherForecastLog bllWeather = new WFIT.BLL.WeatherForecastLog();
WFIT.Model.WeatherForecastLog modelGetWeather = new WFIT.Model.WeatherForecastLog();
WFIT.Model.WeatherForecastLog modelAddWeather = new WFIT.Model.WeatherForecastLog();
modelGetWeather = bllWeather.GetTopOneModel("");
bool bltrue=false;
if (modelGetWeather != null)
{
DateTime NowDtime = DateTime.Now;
DateTime RefreshTime = modelGetWeather.RefreshTime;
TimeSpan timespan = NowDtime.Subtract(RefreshTime);
if (timespan.Hours < 3)
{
strWeather[0] = modelGetWeather.ProvinceCity;
strWeather[1] = modelGetWeather.City;
strWeather[2] = modelGetWeather.CityCode;
strWeather[3] = Convert.ToString(modelGetWeather.RefreshTime);
strWeather[4] = modelGetWeather.METAR;
strWeather[5] = modelGetWeather.AirAndUltravioletRays;
strWeather[6] = modelGetWeather.DressIndex;
strWeather[7] = modelGetWeather.TodayCase;
strWeather[8] = modelGetWeather.TodayT;
strWeather[9] = modelGetWeather.TodayWindDirec;
strWeather[10] = modelGetWeather.TodayPictureOne;
strWeather[11] = modelGetWeather.TodayPictureTwo;
strWeather[12] = modelGetWeather.SecondCase;
strWeather[13] = modelGetWeather.SecondT;
strWeather[14] = modelGetWeather.SecondWindDirec;
strWeather[15] = modelGetWeather.SecondPictureOne;
strWeather[16] = modelGetWeather.SecondPictureTwo;
strWeather[17] = modelGetWeather.ThirdCase;
strWeather[18] = modelGetWeather.ThirdT;
strWeather[19] = modelGetWeather.ThirdWindDirec;
strWeather[20] = modelGetWeather.ThirdPictureOne;
strWeather[21] = modelGetWeather.ThirdPictureTwo;
strWeather[22] = modelGetWeather.FourthCase;
strWeather[23] = modelGetWeather.FourthT;
strWeather[24] = modelGetWeather.FourthWindDirec;
strWeather[25] = modelGetWeather.FourthPictureOne;
strWeather[26] = modelGetWeather.FourthPictureTwo;
strWeather[27] = modelGetWeather.FifthCase;
strWeather[28] = modelGetWeather.FifthT;
strWeather[29] = modelGetWeather.FifthWindDirec;
strWeather[30] = modelGetWeather.FifthPictureOne;
strWeather[31] = modelGetWeather.FifthPictureTwo;
bltrue=true;
}
}
if(bltrue==false)
{
cn.com.webxml.webservice.WeatherWS w = new WebService.cn.com.webxml.webservice.WeatherWS();
strWeather = w.getWeather(citycode, "");
modelAddWeather.ProvinceCity = strWeather[0];
modelAddWeather.City = strWeather[1];
modelAddWeather.CityCode = strWeather[2];
modelAddWeather.RefreshTime = Convert.ToDateTime(strWeather[3]);
modelAddWeather.METAR = strWeather[4];
modelAddWeather.AirAndUltravioletRays = strWeather[5];
modelAddWeather.DressIndex = strWeather[6];
modelAddWeather.TodayCase = strWeather[7];
modelAddWeather.TodayT = strWeather[8];
modelAddWeather.TodayWindDirec = strWeather[9];
modelAddWeather.TodayPictureOne = strWeather[10];
modelAddWeather.TodayPictureTwo = strWeather[11];
modelAddWeather.SecondCase = strWeather[12];
modelAddWeather.SecondT = strWeather[13];
modelAddWeather.SecondWindDirec = strWeather[14];
modelAddWeather.SecondPictureOne = strWeather[15];
modelAddWeather.SecondPictureTwo = strWeather[16];
modelAddWeather.ThirdCase = strWeather[17];
modelAddWeather.ThirdT = strWeather[18];
modelAddWeather.ThirdWindDirec = strWeather[19];
modelAddWeather.ThirdPictureOne = strWeather[20];
modelAddWeather.ThirdPictureTwo = strWeather[21];
modelAddWeather.FourthCase = strWeather[22];
modelAddWeather.FourthT = strWeather[23];
modelAddWeather.FourthWindDirec = strWeather[24];
modelAddWeather.FourthPictureOne = strWeather[25];
modelAddWeather.FourthPictureTwo = strWeather[26];
modelAddWeather.FifthCase = strWeather[27];
modelAddWeather.FifthT = strWeather[28];
modelAddWeather.FifthWindDirec = strWeather[29];
modelAddWeather.FifthPictureOne = strWeather[30];
modelAddWeather.FifthPictureTwo = strWeather[31];
modelAddWeather.CreateTime = DateTime.Now;
if (modelGetWeather == null || (modelGetWeather != null && modelGetWeather.RefreshTime.ToString() != strWeather[3]))
{
bllWeather.Add(modelAddWeather);
}
}
return strWeather;
}
应用(动态调用webserver):
<add key="CityCode" value="2408"/>
<add key="SMSServices" value="SMSServices.asmx"/>
public void GetWeather()
{
string citycode = ConfigurationManager.AppSettings["CityCode"];
string servicename = ConfigurationManager.AppSettings["WeatherServices"]; //webserver名字
object[] args = new object[1]; //参数
args[0] = citycode;
string mes = "";
string date = DateTime.Now.ToString("M月d日");
string[] ss = (string[])Common.Lib.WebServiceHelper.InvokeWebService(servicename, "WeatherOfJiangMen", args); //动态调用
for (int i = 7; i < ss.Length; i++)
{
string str = ss[i];
bool bl = str.Contains(date);
if (bl == true)
{
str = str.Replace(date, "");
mes = date + " 江门天气:" + str + " " + ss[i + 1] + " " + ss[i + 2];
lblWeather.Text = mes;
return;
}
}
lblWeather.Text = "";
}