国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > C# > 正文

C# 站點IP訪問頻率限制 針對單個站點的實現方法

2019-10-29 21:18:16
字體:
來源:轉載
供稿:網友

 站點IP訪問頻率限制 針對單個站點

using System;using System.Collections.Generic;using System.IO;//using System.Linq;using System.Web;// <summary>// IP訪問頻率控制// </summary> public static class IPCacheManager{  /// <summary>  /// IP緩存集合   /// </summary>   private static List<IPCacheInfo> dataList = new List<IPCacheInfo>();  private static object lockObj = new object();  /// <summary>   /// 一段時間內,最大請求次數,必須大于等于1   /// </summary>   private static int maxTimes = 3;  /// <summary>   /// 一段時間長度(單位秒),必須大于等于1   /// </summary>   private static int partSecond = 30;  /// <summary>   /// 請求被拒絕是否加入請求次數   /// </summary>   private static bool isFailAddIn = false;  static IPCacheManager()  {  }  /// <summary>   /// 設置時間,默認maxTimes=3, partSecond=30   /// </summary>   /// <param name="_maxTimes">最大請求次數</param>   /// <param name="_partSecond">請求單位時間</param>   public static void SetTime(int _maxTimes, int _partSecond)  {    maxTimes = _maxTimes;    partSecond = _partSecond;  }  /// <summary>   /// 檢測一段時間內,IP的請求次數是否可以繼續請求   /// 和使用   /// </summary>   /// <param name="ip"></param>   /// <returns></returns>   public static bool CheckIsAble(string ip)  {    lock (lockObj)    {      var item = dataList.Find(p => p.IP == ip);      if (item == null)      {        item = new IPCacheInfo();        item.IP = ip;        item.ReqTime.Add(DateTime.Now);        dataList.Add(item);        return true;      }      else      {        if (item.ReqTime.Count > maxTimes)        {          item.ReqTime.RemoveAt(0);        }        var nowTime = DateTime.Now;        if (isFailAddIn)        {          #region 請求被拒絕也需要加入當次請求          item.ReqTime.Add(nowTime);          if (item.ReqTime.Count >= maxTimes)          {            if (item.ReqTime[0].AddSeconds(partSecond) > nowTime)            {              return false;            }            else            {              return true;            }          }          else          {            return true;          }          #endregion        }        else        {          #region 請求被拒絕就不需要加入當次請求了          if (item.ReqTime.Count >= maxTimes)          {            if (item.ReqTime[0].AddSeconds(partSecond) > nowTime)            {              return false;            }            else            {              item.ReqTime.Add(nowTime);              return true;            }          }          else          {            item.ReqTime.Add(nowTime);            return true;          }          #endregion        }      }    }  }}public class IPCacheInfo{  public string IP { get; set; }  private List<DateTime> reqTime = new List<DateTime>();  public List<DateTime> ReqTime  {    get { return this.reqTime; }    set { this.reqTime = value; }  }}

以上這篇C# 站點IP訪問頻率限制 針對單個站點的實現方法就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支持VEVB武林網。

 

注:相關教程知識閱讀請移步到c#教程頻道。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 达拉特旗| 田阳县| 鹰潭市| 平远县| 灯塔市| 易门县| 曲阜市| 宽城| 瓮安县| 政和县| 信宜市| 临颍县| 青海省| 镇沅| 都昌县| 贵阳市| 遂川县| 东乡县| 衢州市| 隆尧县| 伊春市| 嵊州市| 全州县| 昌邑市| 浦东新区| 安平县| 寿阳县| 永新县| 远安县| 收藏| 沿河| 资源县| 惠安县| 康保县| 新泰市| 金川县| 淮阳县| 厦门市| 渝北区| 宜君县| 灯塔市|