private WeatherHelper() { using (var sr = new StringReader(Properties.Resources.CityKeys)) { string line; while ((line = sr.ReadLine()) != null) { if (!string.IsNullOrEmpty(line)) { var citykey = new CityKeyInfo(line); if (!string.IsNullOrEmpty(citykey.CityKey) && !string.IsNullOrEmpty(citykey.City)) { CityKeyInfos.Add(citykey); } } } } }
/// <summary> /// 更新天气信息 /// </summary> /// <param name="city">城市代码</param> /// <returns></returns> public WeatherInfo UpdateWeather(CityKeyInfo city) { try { if (string.IsNullOrEmpty(city?.CityKey)) { return(null); } HttpWebRequest httpWeather = WebRequest.CreateHttp($"http://zhwnlapi.etouch.cn/Ecalender/api/v2/weather?app_key=99817882&citykey={city.CityKey}"); httpWeather.Timeout = 5000; httpWeather.Method = WebRequestMethods.Http.Get; using (var responseStream = httpWeather.GetResponse().GetResponseStream()) { if (responseStream == null) { return(null); } var weatherData = new StreamReader(new GZipStream(responseStream, CompressionMode.Decompress)); var weatherString = weatherData.ReadToEnd(); var result = JsonConvert.DeserializeObject <WeatherInfo>(weatherString); WeatherInfo = result; WeatherInfoUpdated?.Invoke(); return(WeatherInfo); } } catch { return(null); } }