c# 封装 Request操作类
/// <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> public static string GetHost() { return HttpContext.Current.Request.Url.Host; } /// <summary> /// 得到主机名 /// </summary> public static string GetDnsSafeHost() { return HttpContext.Current.Request.Url.DnsSafeHost; } /// <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(); ; i < BrowserName.Length; i++) { ) return true; } return false; } /// <summary> /// 判断是否来自搜索引擎链接 /// </summary> /// <returns>是否来自搜索引擎链接</returns> public static bool IsSearchEnginesGet() { if (HttpContext.Current.Request.UrlReferrer == null) return false; ", "lycos", "tom", "yisou", "iask", "soso", "gougou", "zhongsou" }; string tmpReferrer = HttpContext.Current.Request.UrlReferrer.ToString().ToLower(); ; i < SearchEngine.Length; i++) { ) 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) { return GetQueryString(strName, false); } /// <summary> /// 获得指定Url参数的值 /// </summary> /// <param name="strName">Url参数</param> /// <param name="sqlSafeCheck">是否进行SQL安全检查</param> /// <returns>Url参数的值</returns> 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) { ); } /// <summary> /// 返回指定URL的参数值(Int型) /// </summary> /// <param name="strName">URL参数</param> /// <param name="defaultvalue">默认值</param> /// <returns>返回指定URL的参数值</returns> 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); } /// <summary> /// 返回指定URL的参数值(String型) /// </summary> /// <param name="strName">URL参数</param> /// <param name="defaultvalue">默认值</param> /// <returns>返回指定URL的参数值</returns> 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; } } /// <summary> /// 获得当前页面的名称 /// </summary> /// <returns>当前页面的名称</returns> public static string GetPageName() { string[] urlArr = HttpContext.Current.Request.Url.AbsolutePath.Split('/'); ].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) { return GetFormString(strName, false); } /// <summary> /// 获得指定表单参数的值 /// </summary> /// <param name="strName">表单参数</param> /// <param name="sqlSafeCheck">是否进行SQL安全检查</param> /// <returns>表单参数的值</returns> 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]; } /// <summary> /// 返回指定表单的参数值(Int型) /// </summary> /// <param name="strName">表单参数</param> /// <returns>返回指定表单的参数值(Int型)</returns> public static int GetFormIntValue(string strName) { ); } /// <summary> /// 返回指定表单的参数值(Int型) /// </summary> /// <param name="strName">表单参数</param> /// <param name="defaultvalue">默认值</param> /// <returns>返回指定表单的参数值</returns> 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; } } /// <summary> /// 返回指定表单的参数值(String型) /// </summary> /// <param name="strName">表单参数</param> /// <returns>返回指定表单的参数值(String型)</returns> public static string GetFormStringValue(string strName) { return GetQueryStringValue(strName, string.Empty); } /// <summary> /// 返回指定表单的参数值(String型) /// </summary> /// <param name="strName">表单参数</param> /// <param name="defaultvalue">默认值</param> /// <returns>返回指定表单的参数值</returns> 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; } } /// <summary> /// 获得Url或表单参数的值, 先判断Url参数是否为空字符串, 如为True则返回表单参数的值 /// </summary> /// <param name="strName">参数</param> /// <returns>Url或表单参数的值</returns> public static string GetString(string strName) { return GetString(strName, false); } /// <summary> /// 获得Url或表单参数的值, 先判断Url参数是否为空字符串, 如为True则返回表单参数的值 /// </summary> /// <param name="strName">参数</param> /// <param name="sqlSafeCheck">是否进行SQL安全检查</param> /// <returns>Url或表单参数的值</returns> 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); } /// <summary> /// 获得Url或表单参数的值, 先判断Url参数是否为空字符串, 如为True则返回表单参数的值 /// </summary> /// <param name="strName">参数</param> /// <param name="sqlSafeCheck">是否进行SQL安全检查</param> /// <returns>Url或表单参数的值</returns> public static string GetStringValue(string strName, string defaultvalue) { if ("".Equals(GetQueryStringValue(strName))) return GetFormStringValue(strName, defaultvalue); else return GetQueryStringValue(strName, defaultvalue); } /// <summary> /// 获得指定Url参数的int类型值 /// </summary> /// <param name="strName">Url参数</param> /// <returns>Url参数的int类型值</returns> public static int GetQueryInt(string 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> /// <returns>表单参数的int类型值</returns> public static int GetFormInt(string strName) { ); } /// <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参数的decimal类型值 /// </summary> /// <param name="strName">Url参数</param> /// <param name="defValue">缺省值</param> /// <returns>Url参数的decimal类型值</returns> public static decimal GetQueryDecimal(string strName, decimal defValue) { return Utils.StrToDecimal(HttpContext.Current.Request.QueryString[strName], defValue); } /// <summary> /// 获得指定表单参数的decimal类型值 /// </summary> /// <param name="strName">表单参数</param> /// <param name="defValue">缺省值</param> /// <returns>表单参数的decimal类型值</returns> public static decimal GetFormDecimal(string strName, decimal defValue) { return Utils.StrToDecimal(HttpContext.Current.Request.Form[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() { 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操作类的更多相关文章
- php--->单例模式封装mysql操作类
php 单例模式封装mysql操作类 单例模式的必要条件(三私一公) 私有的成员属性--防止类外引入这个存放对象的属性 私有的构造方法--为了防止在类外使用new关键字实例化对象 私有的克隆方法--为 ...
- c# 封装Dapper操作类
using Dapper; using DapperExtensions; using System.Collections.Generic; using System.Configuration; ...
- day22:面向对象封装对象操作&类操作&面向对象删除操作
面向对象程序开发 1.类的三种定义方式 class MyClass: pass class MyClass(): #(推荐) pass class MyClass(object): # object类 ...
- php单例模式封装数据库操作类增删改查
<?php//三私一公 单例class Db{ //数据库连接对象 private static $instance; private static $table_name; private $ ...
- Util应用程序框架公共操作类(一):数据类型转换公共操作类(介绍篇)
本系列文章将介绍一些对初学者有帮助的辅助类,这些辅助类本身并没有什么稀奇之处,如何能发现需要封装它们可能更加重要,所谓授之以鱼不如授之以渔,掌握封装公共操作类的技巧才是关键,我会详细说明创建这些类的动 ...
- Util应用程序框架公共操作类
随笔分类 - Util应用程序框架公共操作类 Util应用程序框架公共操作类 Util应用程序框架公共操作类(五):异常公共操作类 摘要: 任何系统都需要处理错误,本文介绍的异常公共操作类,用于对业务 ...
- 基于 Aspose.Cells与XML导入excel 数据----操作类封装
前言 导入excel数据, 在每个项目中基本上都会遇到,第三方插件或者基于微软office,用的最多的就是npoi,aspose.cells和c#基于office这三种方式,其中各有各的优缺点,在这也 ...
- XML序列化 判断是否是手机 字符操作普通帮助类 验证数据帮助类 IO帮助类 c# Lambda操作类封装 C# -- 使用反射(Reflect)获取dll文件中的类型并调用方法 C# -- 文件的压缩与解压(GZipStream)
XML序列化 #region 序列化 /// <summary> /// XML序列化 /// </summary> /// <param name="ob ...
- c# 封装的文件夹操作类之复制文件夹
c# 封装的文件夹操作类之复制文件夹 一.复制文件夹原理: 1.递归遍历文件夹 2.复制文件 二.FolderHelper.cs /// <summary> /// 文件夹操作类 /// ...
随机推荐
- modal 永久移除遮盖层
样式中直接加入: .modal-backdrop { opacity: 0 !important; filter: alpha(opacity=0) !important;}
- 发布本地项目和源码到maven私服中
有时候我们会使用第三方包到我们的项目中,但是想看源码的时候,需要下载源码查看,十分麻烦. 不如把源码上传到maven私服中,这样查看源码的时候就可以直接从mvaen nexus下载直接查看了. 方法如 ...
- Shell脚本-基本运算符
跟着RUNOOB网站的教程学习的笔记 shell和其他编程语言一样,支持多种运算符,包括: 算术运算符 关系运算符 布尔运算符 字符串运算符 文件测试运算符 expr是一款表达式计算公式,使用它能完成 ...
- shell解析my.cnf配置文件
my.cnf配置格式如下 vi my.cnf[client]port=3306socket=/tmp/mysql.socket [mysqld]port=3306server-id=1datadir= ...
- Leeetcode--581. Shortest Unsorted Continuous Subarray
Given an integer array, you need to find one continuous subarray that if you only sort this subarray ...
- lucene的suggest(搜索提示功能的实现)
1.首先引入依赖 <!-- https://mvnrepository.com/artifact/org.apache.lucene/lucene-suggest --> <!-- ...
- oracle utl_http 访问https类型
https://oracle-base.com/articles/misc/utl_http-and-ssl http://blog.whitehorses.nl/2010/05/27/access- ...
- git 删除本地分支、远程分支、本地回滚、远程回滚
一. git 删除分支 1. git 删除本地分支 git branch -D branchname 2. git 删除远程分支 git push origin :branchname (origin ...
- ASP.NET Core 统一异常处理和返回
业务场景: 业务需求要求,需要对 ASP.NET Core 异常进行统一处理和返回,比如出现 500 错误和业务服务错误进行不同的处理和返回. 具体实现: using Microsoft.AspNet ...
- vue keep-alive解决关闭标签动态缓存问题
直接上代码: <keep-alive :include='topNavMentNames'> <router-view ></router-view> </k ...