public void GetJDWeather(object obj) { try { var resultContent = ""; //https://way.jd.com/jisuapi/weather?city=深圳&cityid=111&citycode=101260301&appkey=101260301&appkey=ccba4c1751078e7be9caa133a3c2439c using (DB_TestEntities db = new DB_TestEntities()) { var data = db.Tb_WeatherForecast.ToList(); foreach (var item in data) { using (HttpClient client = new HttpClient()) { var url = "https://way.jd.com/jisuapi/weather?"; var postData = $"city={item.City}&cityid=111&citycode=101260301&appkey=ccba4c1751078e7be9caa133a3c2439c"; var result = client.PostAsync(url + postData, null).Result; resultContent = result.Content.ReadAsStringAsync().Result; //Console.WriteLine(resultContent); } if (item != null) { item.Config = resultContent; item.Verion += 1; item.UpdateDate = DateTime.Now; db.Entry(item).State = System.Data.Entity.EntityState.Modified; } } db.SaveChanges(); } } catch (Exception) { //throw; } }
public ActionResult GetJDWeather(string city) { var resultContent = ""; var state = true; try { using (DB_TestEntities db = new DB_TestEntities()) { var data = db.Tb_WeatherForecast.SingleOrDefault(m => m.City == city); if (data == null) { var url = "https://way.jd.com/jisuapi/weather?"; var postData = $"city={city}&cityid=111&citycode=101260301&appkey=ccba4c1751078e7be9caa133a3c2439c"; using (HttpClient client = new HttpClient()) { //https://way.jd.com/jisuapi/weather?city=深圳&cityid=111&citycode=101260301&appkey=101260301&appkey=ccba4c1751078e7be9caa133a3c2439c var result = client.PostAsync(url + postData, null).Result; resultContent = result.Content.ReadAsStringAsync().Result; } var wf = new Tb_WeatherForecast { Config = resultContent, SaveDate = DateTime.Now, City = city, Verion = 1 }; db.Tb_WeatherForecast.Add(wf); db.SaveChanges(); } else { resultContent = data.Config; } } } catch (Exception) { state = false; //throw; } var o = new { resultContent = resultContent, state = state }; return(Json(o, JsonRequestBehavior.AllowGet)); }