c# 封装 Request操作类

2021-07-09 07:08

阅读:638

标签:search   isnull   etc   默认值   convert   mozilla   ram   request   param   

        /// 
        /// 判断当前页面是否接收到了Post请求
        /// 
        /// 是否接收到了Post请求
        public static bool IsPost()
        {
            return HttpContext.Current.Request.HttpMethod.Equals("POST");
        }

        /// 
        /// 判断当前页面是否接收到了Get请求
        /// 
        /// 是否接收到了Get请求
        public static bool IsGet()
        {
            return HttpContext.Current.Request.HttpMethod.Equals("GET");
        }

        /// 
        /// 返回指定的服务器变量信息
        /// 
        /// 服务器变量名
        /// 服务器变量信息
        public static string GetServerString(string strName)
        {
            if (HttpContext.Current.Request.ServerVariables[strName] == null)
                return "";

            return HttpContext.Current.Request.ServerVariables[strName].ToString();
        }

        /// 
        /// 返回上一个页面的地址
        /// 
        /// 上一个页面的地址
        public static string GetUrlReferrer()
        {
            string retVal = null;

            try
            {
                retVal = HttpContext.Current.Request.UrlReferrer.ToString();
            }
            catch { }

            if (retVal == null)
                return "";

            return retVal;
        }

        /// 
        /// 得到当前完整主机头
        /// 
        /// 
        public static string GetCurrentFullHost()
        {
            HttpRequest request = System.Web.HttpContext.Current.Request;
            if (!request.Url.IsDefaultPort)
                return string.Format("{0}:{1}", request.Url.Host, request.Url.Port.ToString());

            return request.Url.Host;
        }

        /// 
        /// 得到主机头
        /// 
        public static string GetHost()
        {
            return HttpContext.Current.Request.Url.Host;
        }

        /// 
        /// 得到主机名
        /// 
        public static string GetDnsSafeHost()
        {
            return HttpContext.Current.Request.Url.DnsSafeHost;
        }
    

        /// 
        /// 获取当前请求的原始 URL(URL 中域信息之后的部分,包括查询字符串(如果存在))
        /// 
        /// 原始 URL
        public static string GetRawUrl()
        {
            return HttpContext.Current.Request.RawUrl;
        }

        /// 
        /// 判断当前访问是否来自浏览器软件
        /// 
        /// 当前访问是否来自浏览器软件
        public static bool IsBrowserGet()
        {
            string[] BrowserName = { "ie", "opera", "netscape", "mozilla", "konqueror", "firefox" };
            string curBrowser = HttpContext.Current.Request.Browser.Type.ToLower();
            for (int i = 0; i )
            {
                if (curBrowser.IndexOf(BrowserName[i]) >= 0)
                    return true;
            }
            return false;
        }

        /// 
        /// 判断是否来自搜索引擎链接
        /// 
        /// 是否来自搜索引擎链接
        public static bool IsSearchEnginesGet()
        {
            if (HttpContext.Current.Request.UrlReferrer == null)
                return false;

            string[] SearchEngine = { "google", "yahoo", "msn", "baidu", "sogou", "sohu", "sina", "163", "lycos", "tom", "yisou", "iask", "soso", "gougou", "zhongsou" };
            string tmpReferrer = HttpContext.Current.Request.UrlReferrer.ToString().ToLower();
            for (int i = 0; i )
            {
                if (tmpReferrer.IndexOf(SearchEngine[i]) >= 0)
                    return true;
            }
            return false;
        }

        /// 
        /// 获得当前完整Url地址
        /// 
        /// 当前完整Url地址
        public static string GetUrl()
        {
            return HttpContext.Current.Request.Url.ToString();
        }

        /// 
        /// 获得指定Url参数的值
        /// 
        /// Url参数
        /// Url参数的值
        public static string GetQueryString(string strName)
        {
            return GetQueryString(strName, false);
        }

        /// 
        /// 获得指定Url参数的值
        ///  
        /// Url参数
        /// 是否进行SQL安全检查
        /// Url参数的值
        public static string GetQueryString(string strName, bool sqlSafeCheck)
        {
            if (HttpContext.Current.Request.QueryString[strName] == null)
                return "";

            if (sqlSafeCheck && !Utils.IsSafeSqlString(HttpContext.Current.Request.QueryString[strName]))
                return "unsafe string";

            return HttpContext.Current.Request.QueryString[strName];
        }


        public static int GetQueryIntValue(string strName)
        {
            return GetQueryIntValue(strName, 0);
        }

        /// 
        /// 返回指定URL的参数值(Int型)
        /// 
        /// URL参数
        /// 默认值
        /// 返回指定URL的参数值
        public static int GetQueryIntValue(string strName, int defaultvalue)
        {
            if (HttpContext.Current.Request.QueryString[strName] == null || HttpContext.Current.Request.QueryString[strName].ToString() == string.Empty)
                return defaultvalue;
            else
            {
                Regex obj = new Regex("\\d+");
                Match objmach = obj.Match(HttpContext.Current.Request.QueryString[strName].ToString());
                if (objmach.Success)
                    return Convert.ToInt32(objmach.Value);
                else
                    return defaultvalue;
            }
        }


        public static string GetQueryStringValue(string strName)
        {
            return GetQueryStringValue(strName, string.Empty);
        }

        /// 
        /// 返回指定URL的参数值(String型)
        /// 
        /// URL参数
        /// 默认值
        /// 返回指定URL的参数值
        public static string GetQueryStringValue(string strName, string defaultvalue)
        {
            if (HttpContext.Current.Request.QueryString[strName] == null || HttpContext.Current.Request.QueryString[strName].ToString() == string.Empty)
                return defaultvalue;
            else
            {
                Regex obj = new Regex("\\w+");
                Match objmach = obj.Match(HttpContext.Current.Request.QueryString[strName].ToString());
                if (objmach.Success)
                    return objmach.Value;
                else
                    return defaultvalue;
            }
        }
        /// 
        /// 获得当前页面的名称
        /// 
        /// 当前页面的名称
        public static string GetPageName()
        {
            string[] urlArr = HttpContext.Current.Request.Url.AbsolutePath.Split(/);
            return urlArr[urlArr.Length - 1].ToLower();
        }

        /// 
        /// 返回表单或Url参数的总个数
        /// 
        /// 
        public static int GetParamCount()
        {
            return HttpContext.Current.Request.Form.Count + HttpContext.Current.Request.QueryString.Count;
        }

        /// 
        /// 获得指定表单参数的值
        /// 
        /// 表单参数
        /// 表单参数的值
        public static string GetFormString(string strName)
        {
            return GetFormString(strName, false);
        }

        /// 
        /// 获得指定表单参数的值
        /// 
        /// 表单参数
        /// 是否进行SQL安全检查
        /// 表单参数的值
        public static string GetFormString(string strName, bool sqlSafeCheck)
        {
            if (HttpContext.Current.Request.Form[strName] == null)
                return "";

            if (sqlSafeCheck && !Utils.IsSafeSqlString(HttpContext.Current.Request.Form[strName]))
                return "unsafe string";

            return HttpContext.Current.Request.Form[strName];
        }
        /// 
        /// 返回指定表单的参数值(Int型)
        /// 
        /// 表单参数
        /// 返回指定表单的参数值(Int型)
        public static int GetFormIntValue(string strName)
        {
            return GetFormIntValue(strName, 0);
        }
        /// 
        /// 返回指定表单的参数值(Int型)
        /// 
        /// 表单参数
        /// 默认值
        /// 返回指定表单的参数值
        public static int GetFormIntValue(string strName, int defaultvalue)
        {
            if (HttpContext.Current.Request.Form[strName] == null || HttpContext.Current.Request.Form[strName].ToString() == string.Empty)
                return defaultvalue;
            else
            {
                Regex obj = new Regex("\\d+");
                Match objmach = obj.Match(HttpContext.Current.Request.Form[strName].ToString());
                if (objmach.Success)
                    return Convert.ToInt32(objmach.Value);
                else
                    return defaultvalue;
            }
        }
        /// 
        /// 返回指定表单的参数值(String型)
        /// 
        /// 表单参数
        /// 返回指定表单的参数值(String型)
        public static string GetFormStringValue(string strName)
        {
            return GetQueryStringValue(strName, string.Empty);
        }
        /// 
        /// 返回指定表单的参数值(String型)
        /// 
        /// 表单参数
        /// 默认值
        /// 返回指定表单的参数值
        public static string GetFormStringValue(string strName, string defaultvalue)
        {
            if (HttpContext.Current.Request.Form[strName] == null || HttpContext.Current.Request.Form[strName].ToString() == string.Empty)
                return defaultvalue;
            else
            {
                Regex obj = new Regex("\\w+");
                Match objmach = obj.Match(HttpContext.Current.Request.Form[strName].ToString());
                if (objmach.Success)
                    return objmach.Value;
                else
                    return defaultvalue;
            }
        }

        /// 
        /// 获得Url或表单参数的值, 先判断Url参数是否为空字符串, 如为True则返回表单参数的值
        /// 
        /// 参数
        /// Url或表单参数的值
        public static string GetString(string strName)
        {
            return GetString(strName, false);
        }
      

        /// 
        /// 获得Url或表单参数的值, 先判断Url参数是否为空字符串, 如为True则返回表单参数的值
        /// 
        /// 参数
        /// 是否进行SQL安全检查
        /// Url或表单参数的值
        public static string GetString(string strName, bool sqlSafeCheck)
        {
            if ("".Equals(GetQueryString(strName)))
                return GetFormString(strName, sqlSafeCheck);
            else
                return GetQueryString(strName, sqlSafeCheck);
        }
        public static string GetStringValue(string strName)
        {
                 return GetStringValue(strName, string.Empty);
        }
        /// 
        /// 获得Url或表单参数的值, 先判断Url参数是否为空字符串, 如为True则返回表单参数的值
        /// 
        /// 参数
        /// 是否进行SQL安全检查
        /// Url或表单参数的值
        public static string GetStringValue(string strName, string defaultvalue)
        {
            if ("".Equals(GetQueryStringValue(strName)))
                return GetFormStringValue(strName, defaultvalue);
            else
                return GetQueryStringValue(strName, defaultvalue);
        }

        /// 
        /// 获得指定Url参数的int类型值
        /// 
        /// Url参数
        /// Url参数的int类型值
        public static int GetQueryInt(string strName)
        {
            return Utils.StrToInt(HttpContext.Current.Request.QueryString[strName], 0);
        }

        /// 
        /// 获得指定Url参数的int类型值
        /// 
        /// Url参数
        /// 缺省值
        /// Url参数的int类型值
        public static int GetQueryInt(string strName, int defValue)
        {
            return Utils.StrToInt(HttpContext.Current.Request.QueryString[strName], defValue);
        }

        /// 
        /// 获得指定表单参数的int类型值
        /// 
        /// 表单参数
        /// 表单参数的int类型值
        public static int GetFormInt(string strName)
        {
            return GetFormInt(strName, 0);
        }

        /// 
        /// 获得指定表单参数的int类型值
        /// 
        /// 表单参数
        /// 缺省值
        /// 表单参数的int类型值
        public static int GetFormInt(string strName, int defValue)
        {
            return Utils.StrToInt(HttpContext.Current.Request.Form[strName], defValue);
        }

        /// 
        /// 获得指定Url或表单参数的int类型值, 先判断Url参数是否为缺省值, 如为True则返回表单参数的值
        /// 
        /// Url或表单参数
        /// 缺省值
        /// Url或表单参数的int类型值
        public static int GetInt(string strName, int defValue)
        {
            if (GetQueryInt(strName, defValue) == defValue)
                return GetFormInt(strName, defValue);
            else
                return GetQueryInt(strName, defValue);
        }

        /// 
        /// 获得指定Url参数的decimal类型值
        /// 
        /// Url参数
        /// 缺省值
        /// Url参数的decimal类型值
        public static decimal GetQueryDecimal(string strName, decimal defValue)
        {
            return Utils.StrToDecimal(HttpContext.Current.Request.QueryString[strName], defValue);
        }

        /// 
        /// 获得指定表单参数的decimal类型值
        /// 
        /// 表单参数
        /// 缺省值
        /// 表单参数的decimal类型值
        public static decimal GetFormDecimal(string strName, decimal defValue)
        {
            return Utils.StrToDecimal(HttpContext.Current.Request.Form[strName], defValue);
        }

        /// 
        /// 获得指定Url参数的float类型值
        /// 
        /// Url参数
        /// 缺省值
        /// Url参数的int类型值
        public static float GetQueryFloat(string strName, float defValue)
        {
            return Utils.StrToFloat(HttpContext.Current.Request.QueryString[strName], defValue);
        }

        /// 
        /// 获得指定表单参数的float类型值
        /// 
        /// 表单参数
        /// 缺省值
        /// 表单参数的float类型值
        public static float GetFormFloat(string strName, float defValue)
        {
            return Utils.StrToFloat(HttpContext.Current.Request.Form[strName], defValue);
        }

        /// 
        /// 获得指定Url或表单参数的float类型值, 先判断Url参数是否为缺省值, 如为True则返回表单参数的值
        /// 
        /// Url或表单参数
        /// 缺省值
        /// Url或表单参数的int类型值
        public static float GetFloat(string strName, float defValue)
        {
            if (GetQueryFloat(strName, defValue) == defValue)
                return GetFormFloat(strName, defValue);
            else
                return GetQueryFloat(strName, defValue);
        }

        /// 
        /// 获得当前页面客户端的IP
        /// 
        /// 当前页面客户端的IP
        public static string GetIP()
        {
            try
            {
                string result = String.Empty;

                result = HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
                if (string.IsNullOrEmpty(result))
                {
                    result = HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"];
                }

                if (string.IsNullOrEmpty(result))
                {
                    result = HttpContext.Current.Request.UserHostAddress;
                }

                if (string.IsNullOrEmpty(result) || !Utils.IsIP(result))
                {
                    return "127.0.0.1";
                }

                return result;
            }
            catch (Exception ex)
            {
                System.Diagnostics.Trace.Write(ex.Message);
                return HttpContext.Current.Request.UserHostAddress;
            }
        

 

c# 封装 Request操作类

标签:search   isnull   etc   默认值   convert   mozilla   ram   request   param   

原文地址:https://www.cnblogs.com/yuanzijian-ruiec/p/9715568.html


评论


亲,登录后才可以留言!