• ASP.NET Request对象


    ASP.NET Request对象

    常用的代码信息我都贴出来了,自己看看吧

    using System;
    using System.Web;
    using System.Text.RegularExpressions;
    
    namespace WMSWeb.Common
    {
        /// <summary>
        /// Request操作类
        /// </summary>
        public class WMSRequest
        {
            /// <summary>
            /// 判断当前页面是否接收到了Post请求
            /// </summary>
            /// <returns>是否接收到了Post请求</returns>
            public static bool IsPost()
            {
                return HttpContext.Current.Request.HttpMethod.Equals("POST");
            }
            /// <summary>
            /// 判断当前页面是否接收到了Get请求
            /// </summary>
            /// <returns>是否接收到了Get请求</returns>
            public static bool IsGet()
            {
                return HttpContext.Current.Request.HttpMethod.Equals("GET");
            }
    
            /// <summary>
            /// 返回指定的服务器变量信息
            /// </summary>
            /// <param name="strName">服务器变量名</param>
            /// <returns>服务器变量信息</returns>
            public static string GetServerString(string strName)
            {
                //
                if (HttpContext.Current.Request.ServerVariables[strName] == null)
                {
                    return "";
                }
                return HttpContext.Current.Request.ServerVariables[strName].ToString();
            }
    
            /// <summary>
            /// 返回上一个页面的地址
            /// </summary>
            /// <returns>上一个页面的地址</returns>
            public static string GetUrlReferrer()
            {
                string retVal = null;
    
                try
                {
                    retVal = HttpContext.Current.Request.UrlReferrer.ToString();
                }
                catch { }
    
                if (retVal == null)
                    return "";
    
                return retVal;
    
            }
    
            /// <summary>
            /// 得到当前完整主机头
            /// </summary>
            /// <returns></returns>
            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;
            }
    
            /// <summary>
            /// 得到主机头
            /// </summary>
            /// <returns></returns>
            public static string GetHost()
            {
                return HttpContext.Current.Request.Url.Host;
            }
    
    
            /// <summary>
            /// 获取当前请求的原始 URL(URL 中域信息之后的部分,包括查询字符串(如果存在))
            /// </summary>
            /// <returns>原始 URL</returns>
            public static string GetRawUrl()
            {
                return HttpContext.Current.Request.RawUrl;
            }
    
            /// <summary>
            /// 判断当前访问是否来自浏览器软件
            /// </summary>
            /// <returns>当前访问是否来自浏览器软件</returns>
            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 < BrowserName.Length; i++)
                {
                    if (curBrowser.IndexOf(BrowserName[i]) >= 0)
                    {
                        return true;
                    }
                }
                return false;
            }
    
            /// <summary>
            /// 判断是否来自搜索引擎链接
            /// </summary>
            /// <returns>是否来自搜索引擎链接</returns>
            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 < SearchEngine.Length; i++)
                {
                    if (tmpReferrer.IndexOf(SearchEngine[i]) >= 0)
                    {
                        return true;
                    }
                }
                return false;
            }
    
            /// <summary>
            /// 获得当前完整Url地址
            /// </summary>
            /// <returns>当前完整Url地址</returns>
            public static string GetUrl()
            {
                return HttpContext.Current.Request.Url.ToString();
            }
    
    
            /// <summary>
            /// 获得指定Url参数的值
            /// </summary>
            /// <param name="strName">Url参数</param>
            /// <returns>Url参数的值</returns>
            public static string GetQueryString(string strName)
            {
                if (HttpContext.Current.Request.QueryString[strName] == null)
                {
                    return "";
                }
                return SqlFix(HttpContext.Current.Request.QueryString[strName]);
            }
    
            /// <summary>
            /// 获得当前页面的名称
            /// </summary>
            /// <returns>当前页面的名称</returns>
            public static string GetPageName()
            {
                string[] urlArr = HttpContext.Current.Request.Url.AbsolutePath.Split('/');
                return urlArr[urlArr.Length - 1].ToLower();
            }
    
            /// <summary>
            /// 返回表单或Url参数的总个数
            /// </summary>
            /// <returns></returns>
            public static int GetParamCount()
            {
                return HttpContext.Current.Request.Form.Count + HttpContext.Current.Request.QueryString.Count;
            }
    
    
            /// <summary>
            /// 获得指定表单参数的值
            /// </summary>
            /// <param name="strName">表单参数</param>
            /// <returns>表单参数的值</returns>
            public static string GetFormString(string strName)
            {
                if (HttpContext.Current.Request.Form[strName] == null)
                {
                    return "";
                }
                return SqlFix(HttpContext.Current.Request.Form[strName]);
            }
    
            /// <summary>
            /// 获得Url或表单参数的值, 先判断Url参数是否为空字符串, 如为True则返回表单参数的值
            /// </summary>
            /// <param name="strName">参数</param>
            /// <returns>Url或表单参数的值</returns>
            public static string GetString(string strName)
            {
                var value = GetQueryString(strName);
                if (String.IsNullOrEmpty(value))
                {
                    return GetFormString(strName);
                }
                return value;
            }
    
            /// <summary>
            /// 过滤Sql关键字
            /// </summary>
            /// <param name="strName"></param>
            /// <returns></returns>
            public static string SqlFix(string strName)
            {
                strName = strName.Replace("+", "");
                strName = strName.Replace("--", "--");
                strName = strName.Replace("/*", "/*");
                strName = strName.Replace("*/", "*/");
                strName = strName.Replace("0x", "0x");
                strName = strName.Replace("'", "''");
                //Regex r = null;
                Match m = null;
                Regex RegexBr = new Regex(@"(delete)", RegexOptions.IgnoreCase);
                for (m = RegexBr.Match(strName); m.Success; m = m.NextMatch())
                {
                    strName = strName.Replace(m.Groups[0].ToString(), "");
                }
    
                m = null;
                RegexBr = new Regex(@"(drop)", RegexOptions.IgnoreCase);
                for (m = RegexBr.Match(strName); m.Success; m = m.NextMatch())
                {
                    strName = strName.Replace(m.Groups[0].ToString(), "");
                }
    
                m = null;
                RegexBr = new Regex(@"(insert)", RegexOptions.IgnoreCase);
                for (m = RegexBr.Match(strName); m.Success; m = m.NextMatch())
                {
                    strName = strName.Replace(m.Groups[0].ToString(), "");
                }
    
                m = null;
                RegexBr = new Regex(@"(select)", RegexOptions.IgnoreCase);
                for (m = RegexBr.Match(strName); m.Success; m = m.NextMatch())
                {
                    strName = strName.Replace(m.Groups[0].ToString(), "");
                }
    
                //m = null;
                //RegexBr = new Regex(@"(update)", RegexOptions.IgnoreCase);
                //for (m = RegexBr.Match(strName); m.Success; m = m.NextMatch())
                //{
                //    strName = strName.Replace(m.Groups[0].ToString(), "");
                //}
    
                m = null;
                RegexBr = new Regex(@"(grant)", RegexOptions.IgnoreCase);
                for (m = RegexBr.Match(strName); m.Success; m = m.NextMatch())
                {
                    strName = strName.Replace(m.Groups[0].ToString(), "");
                }
    
                m = null;
                RegexBr = new Regex(@"(declare)", RegexOptions.IgnoreCase);
                for (m = RegexBr.Match(strName); m.Success; m = m.NextMatch())
                {
                    strName = strName.Replace(m.Groups[0].ToString(), "");
                }
    
                m = null;
                RegexBr = new Regex(@"(exec)", RegexOptions.IgnoreCase);
                for (m = RegexBr.Match(strName); m.Success; m = m.NextMatch())
                {
                    strName = strName.Replace(m.Groups[0].ToString(), "");
                }
    
                return strName;
            }
    
    
            /// <summary>
            /// 获得指定Url参数的int类型值
            /// </summary>
            /// <param name="strName">Url参数</param>
            /// <param name="defValue">缺省值</param>
            /// <returns>Url参数的int类型值</returns>
            public static int GetQueryInt(string strName, int defValue)
            {
                return Utils.StrToInt(HttpContext.Current.Request.QueryString[strName], defValue);
            }
    
    
            /// <summary>
            /// 获得指定表单参数的int类型值
            /// </summary>
            /// <param name="strName">表单参数</param>
            /// <param name="defValue">缺省值</param>
            /// <returns>表单参数的int类型值</returns>
            public static int GetFormInt(string strName, int defValue)
            {
                return Utils.StrToInt(HttpContext.Current.Request.Form[strName], defValue);
            }
    
            /// <summary>
            /// 获得指定Url或表单参数的int类型值, 先判断Url参数是否为缺省值, 如为True则返回表单参数的值
            /// </summary>
            /// <param name="strName">Url或表单参数</param>
            /// <param name="defValue">缺省值</param>
            /// <returns>Url或表单参数的int类型值</returns>
            public static int GetInt(string strName, int defValue)
            {
                if (GetQueryInt(strName, defValue) == defValue)
                {
                    return GetFormInt(strName, defValue);
                }
                else
                {
                    return GetQueryInt(strName, defValue);
                }
            }
    
            /// <summary>
            /// 获得指定Url参数的float类型值
            /// </summary>
            /// <param name="strName">Url参数</param>
            /// <param name="defValue">缺省值</param>
            /// <returns>Url参数的int类型值</returns>
            public static float GetQueryFloat(string strName, float defValue)
            {
                return Utils.StrToFloat(HttpContext.Current.Request.QueryString[strName], defValue);
            }
    
    
            /// <summary>
            /// 获得指定表单参数的float类型值
            /// </summary>
            /// <param name="strName">表单参数</param>
            /// <param name="defValue">缺省值</param>
            /// <returns>表单参数的float类型值</returns>
            public static float GetFormFloat(string strName, float defValue)
            {
                return Utils.StrToFloat(HttpContext.Current.Request.Form[strName], defValue);
            }
    
            /// <summary>
            /// 获得指定Url或表单参数的float类型值, 先判断Url参数是否为缺省值, 如为True则返回表单参数的值
            /// </summary>
            /// <param name="strName">Url或表单参数</param>
            /// <param name="defValue">缺省值</param>
            /// <returns>Url或表单参数的int类型值</returns>
            public static float GetFloat(string strName, float defValue)
            {
                if (GetQueryFloat(strName, defValue) == defValue)
                {
                    return GetFormFloat(strName, defValue);
                }
                else
                {
                    return GetQueryFloat(strName, defValue);
                }
            }
    
            /// <summary>
            /// 获得当前页面客户端的IP
            /// </summary>
            /// <returns>当前页面客户端的IP</returns>
            public static string GetIP()
            {
    
    
                string result = String.Empty;
    
                result = HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
                if (null == result || result == String.Empty)
                {
                    result = HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"];
                }
    
                if (null == result || result == String.Empty)
                {
                    result = HttpContext.Current.Request.UserHostAddress;
                }
    
                if (null == result || result == String.Empty || !Utils.IsIP(result))
                {
                    return "0.0.0.0";
                }
    
                return result;
    
            }
    
            /// <summary>
            /// 保存用户上传的文件
            /// </summary>
            /// <param name="path">保存路径</param>
            public static void SaveRequestFile(string path)
            {
                if (HttpContext.Current.Request.Files.Count > 0)
                {
                    HttpContext.Current.Request.Files[0].SaveAs(path);
                }
            }
    
            /// <summary>
            /// Textarea输入内容转换成Html格式
            /// </summary>
            /// <param name="strName">参数</param>
            /// <returns>Url或表单参数的值</returns>
            public static string Text2Html(string strName)
            {
                //strName = strName.Replace("
    ", "<br>");
                strName = strName.Replace("
    ", "<br>");
                strName = strName.Replace("
    ", "<br>");
                strName = strName.Replace("<br><br>", "<br>");
                strName = strName.Replace("<br><br>", "<br>");
                return strName;
            }
            public static string Html2Text(string strName)
            {
                strName = strName.Replace("<br>", "
    ");
                return strName;
            }
    
            public static string GetScript(string scrip)
            {
                return String.Format("<script>{0}</script>", scrip);
            }
    
            //        /// <summary>
            //        /// 保存上传的文件
            //        /// </summary>
            //        /// <param name="MaxAllowFileCount">最大允许的上传文件个数</param>
            //        /// <param name="MaxAllowFileSize">最大允许的文件长度(单位: KB)</param>
            //        /// <param name="AllowFileExtName">允许的文件扩展名, 以string[]形式提供</param>
            //        /// <param name="AllowFileType">允许的文件类型, 以string[]形式提供</param>
            //        /// <param name="Dir">目录</param>
            //        /// <returns></returns>
            //        public static Forum.AttachmentInfo[] SaveRequestFiles(int MaxAllowFileCount, int MaxAllowFileSize, string[] AllowFileExtName, string[] AllowFileType, string Dir)
            //        {
            //            int savefilecount = 0;
            //            
            //            int fcount = Math.Min(MaxAllowFileCount, HttpContext.Current.Request.Files.Count);
            //
            //            Forum.AttachmentInfo[] attachmentinfo = new Forum.AttachmentInfo[fcount];
            //            for(int i=0;i<fcount;i++)
            //            {
            //                string filename = HttpContext.Current.Request.Files[i].FileName;
            //                string fileextname = filename.Substring(filename.LastIndexOf("."));
            //                string filetype = HttpContext.Current.Request.Files[i].ContentType;
            //                int filesize = HttpContext.Current.Request.Files[i].ContentLength;
            //                // 判断 文件扩展名/文件大小/文件类型 是否符合要求
            //                if(Utils.InArray(fileextname, AllowFileExtName) && (filesize <= MaxAllowFileSize * 1024) && Utils.InArray(filetype, AllowFileType))
            //                {
            //
            //                    HttpContext.Current.Request.Files[i].SaveAs(Dir + Utils.GetDateTime() + Environment.TickCount.ToString() + fileextname);
            //                    attachmentinfo[savefilecount].Filename = filename;
            //                    attachmentinfo[savefilecount].Filesize = filesize;
            //                    attachmentinfo[savefilecount].Description = filetype;
            //                    attachmentinfo[savefilecount].Filetype = fileextname;
            //                    savefilecount++;
            //                }
            //            }
            //            return attachmentinfo;
            //            
            //        }
    
        }
    }
  • 相关阅读:
    PHP 实现定时任务的几种方法
    ueditor 文本编辑器
    CodeIgniter 如何去掉 Index.php
    php 后台权限例子 (mysql 数据表)
    php ajax 下拉加载数据
    Codeforces 931.D Peculiar apple-tree
    Codeforces 931.C Laboratory Work
    Codeforces 932.F Escape Through Leaf
    bzoj2325 [ZJOI2011]道馆之战
    bzoj3611 [Heoi2014]大工程
  • 原文地址:https://www.cnblogs.com/mc67/p/4812337.html
Copyright © 2020-2023  润新知