Beispiel #1
0
        public JsonResult SyncHolday()
        {
            JsonMessage jm = new JsonMessage();
            MyHttpUtility http = new MyHttpUtility();
            DateTime queryTime = DateTime.Today;
            DateTime maxTime = DateTime.Today.AddMonths(1);
            while (queryTime < maxTime)
            {
                int tryCount = 10;
                while (true)
                {
                    try
                    {
                        using (DBContext context = new DBContext())
                        {
                            string queryDate = queryTime.ToString("yyyy-MM-dd");
                            string timeJson = http.DoGet("http://www.easybots.cn/api/holiday.php?d=" + queryDate);
                            if (string.IsNullOrEmpty(timeJson)) throw new Exception("节假日返回值为空");
                            string[] tempArr = timeJson.Substring(1, timeJson.Length - 2).Split(':');
                            if (tempArr.Length != 2) throw new Exception("节假日返回值有变动");
                            int val = Convert.ToInt32(tempArr[1].Replace('\"', ' ').Trim());
                            //写到数据为
                            string sql = "update Holiday set isHoliday=" + val + " where day = '" + queryDate + "'";
                            int cc = context.ExecuteCommand(sql);
                            if (cc == 0)
                            {
                                sql = "insert into Holiday(day,isHoliday) values('" + queryDate + "'," + val + " )";
                                context.ExecuteCommand(sql);

                            }
                        }
                        break;
                    }
                    catch (System.Net.WebException ex)
                    {
                        tryCount--;
                        if (tryCount < 0) { jm.LogException(ex); break; }
                        LogError("使用网络节假日发生网络异常,重试" + tryCount, ex);
                    }
                    catch (Exception ex)
                    {
                        jm.LogException(ex);
                    }
                    break;
                }
                queryTime = queryTime.AddDays(1);
            }
            return Json(jm, JsonRequestBehavior.AllowGet);
        }