地铁二期项目正式开始

DNTRequest.cs 9.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403
  1. /*
  2. * 版权所有:Copyright (C) 2013,加一信息科技
  3.   * Version: 1.0
  4.   * 创建日期: 2013-05-16
  5.   * 作者: 张琨
  6.   * 说明: 和Request相关的方法
  7.   * 修改日期: yyyy-MM-dd
  8.   * 修改者: 修改者
  9.   * 修改说明: 修改说明
  10. */
  11. using System;
  12. using System.Web;
  13. using System.Collections.Generic;
  14. using System.Text;
  15. using System.Text.RegularExpressions;
  16. namespace YTSoft.Common
  17. {
  18. /// <summary>
  19. /// Request操作类
  20. /// </summary>
  21. public class DNTRequest
  22. {
  23. /// <summary>
  24. /// 判断当前页面是否接收到了Post请求
  25. /// </summary>
  26. /// <returns>是否接收到了Post请求</returns>
  27. public static bool IsPost()
  28. {
  29. return HttpContext.Current.Request.HttpMethod.ToUpper().Equals("POST");
  30. }
  31. /// <summary>
  32. /// 判断当前页面是否接收到了Get请求
  33. /// </summary>
  34. /// <returns>是否接收到了Get请求</returns>
  35. public static bool IsGet()
  36. {
  37. return HttpContext.Current.Request.HttpMethod.ToUpper().Equals("GET");
  38. }
  39. /// <summary>
  40. /// 获取参数信息(form或get提交)
  41. /// </summary>
  42. /// <param name="Param"></param>
  43. /// <returns></returns>
  44. public static string GetParamString(string Param)
  45. {
  46. if (IsPost())
  47. {
  48. return GetFormString(Param);
  49. }
  50. else if (IsGet())
  51. {
  52. return GetQueryString(Param);
  53. }
  54. return "";
  55. }
  56. /// <summary>
  57. /// 获取参数数字信息(form或get提交)
  58. /// </summary>
  59. /// <param name="Param"></param>
  60. /// <returns></returns>
  61. public static int GetParamNumber(string Param)
  62. {
  63. if (IsPost())
  64. {
  65. return GetFormNumber(Param);
  66. }
  67. else if (IsGet())
  68. {
  69. return GetQueryNumber(Param);
  70. }
  71. return -1;
  72. }
  73. /// <summary>
  74. /// 获取参数数字信息(form或get提交)
  75. /// </summary>
  76. /// <param name="Param"></param>
  77. /// <returns></returns>
  78. public static decimal GetParamDecimal(string Param)
  79. {
  80. if (IsPost())
  81. {
  82. return GetFormDecimal(Param);
  83. }
  84. else if (IsGet())
  85. {
  86. return GetQueryDecimal(Param);
  87. }
  88. return -1;
  89. }
  90. /// <summary>
  91. /// 返回指定的服务器变量信息
  92. /// </summary>
  93. /// <param name="strName">服务器变量名</param>
  94. /// <returns>服务器变量信息</returns>
  95. public static string GetServerString(string strName)
  96. {
  97. //
  98. if (HttpContext.Current.Request.ServerVariables[strName] == null)
  99. {
  100. return "";
  101. }
  102. return HttpContext.Current.Request.ServerVariables[strName].ToString();
  103. }
  104. /// <summary>
  105. /// 返回上一个页面的地址
  106. /// </summary>
  107. /// <returns>上一个页面的地址</returns>
  108. public static string GetUrlReferrer()
  109. {
  110. string retVal = null;
  111. try
  112. {
  113. retVal = HttpContext.Current.Request.UrlReferrer.ToString();
  114. }
  115. catch{}
  116. if (retVal == null)
  117. return "";
  118. return retVal;
  119. }
  120. /// <summary>
  121. /// 得到当前完整主机头
  122. /// </summary>
  123. /// <returns></returns>
  124. public static string GetCurrentFullHost()
  125. {
  126. HttpRequest request = System.Web.HttpContext.Current.Request;
  127. if (!request.Url.IsDefaultPort)
  128. {
  129. return string.Format("{0}:{1}", request.Url.Host, request.Url.Port.ToString());
  130. }
  131. return request.Url.Host;
  132. }
  133. /// <summary>
  134. /// 得到主机头
  135. /// </summary>
  136. /// <returns></returns>
  137. public static string GetHost()
  138. {
  139. return HttpContext.Current.Request.Url.Host;
  140. }
  141. /// <summary>
  142. /// 获取当前请求的原始 URL(URL 中域信息之后的部分,包括查询字符串(如果存在))
  143. /// </summary>
  144. /// <returns>原始 URL</returns>
  145. public static string GetRawUrl()
  146. {
  147. return HttpContext.Current.Request.RawUrl;
  148. }
  149. /// <summary>
  150. /// 判断当前访问是否来自浏览器软件
  151. /// </summary>
  152. /// <returns>当前访问是否来自浏览器软件</returns>
  153. public static bool IsBrowserGet()
  154. {
  155. string[] BrowserName = {"ie", "opera", "netscape", "mozilla", "konqueror", "firefox"};
  156. string curBrowser = HttpContext.Current.Request.Browser.Type.ToLower();
  157. for (int i = 0; i < BrowserName.Length; i++)
  158. {
  159. if (curBrowser.IndexOf(BrowserName[i]) >= 0)
  160. {
  161. return true;
  162. }
  163. }
  164. return false;
  165. }
  166. /// <summary>
  167. /// 判断是否来自搜索引擎链接
  168. /// </summary>
  169. /// <returns>是否来自搜索引擎链接</returns>
  170. public static bool IsSearchEnginesGet()
  171. {
  172. if (HttpContext.Current.Request.UrlReferrer == null)
  173. {
  174. return false;
  175. }
  176. string[] SearchEngine = {"google", "yahoo", "msn", "baidu", "sogou", "sohu", "sina", "163", "lycos", "tom", "yisou", "iask", "soso", "gougou", "zhongsou"};
  177. string tmpReferrer = HttpContext.Current.Request.UrlReferrer.ToString().ToLower();
  178. for (int i = 0; i < SearchEngine.Length; i++)
  179. {
  180. if (tmpReferrer.IndexOf(SearchEngine[i]) >= 0)
  181. {
  182. return true;
  183. }
  184. }
  185. return false;
  186. }
  187. /// <summary>
  188. /// 获得当前完整Url地址
  189. /// </summary>
  190. /// <returns>当前完整Url地址</returns>
  191. public static string GetUrl()
  192. {
  193. return HttpContext.Current.Request.Url.ToString();
  194. }
  195. /// <summary>
  196. /// 获得指定Url参数的值
  197. /// </summary>
  198. /// <param name="strName">Url参数</param>
  199. /// <returns>Url参数的值</returns>
  200. public static string GetQueryString(string strName)
  201. {
  202. if (HttpContext.Current.Request.QueryString[strName] == null)
  203. {
  204. return "";
  205. }
  206. return HttpContext.Current.Request.QueryString[strName];
  207. }
  208. /// <summary>
  209. /// 获取指定的URL参数数字值(返回整形)
  210. /// </summary>
  211. /// <param name="strName"></param>
  212. /// <returns></returns>
  213. public static int GetQueryNumber(string strName)
  214. {
  215. string QueryValue = HttpContext.Current.Request.QueryString[strName];
  216. if (string.IsNullOrEmpty(QueryValue))
  217. {
  218. return -1;
  219. }
  220. else
  221. {
  222. try
  223. {
  224. return Convert.ToInt32(QueryValue);
  225. }
  226. catch
  227. {
  228. return -1;
  229. }
  230. }
  231. }
  232. /// <summary>
  233. /// 获取指定的URL参数数字值(返回十进制数)
  234. /// </summary>
  235. /// <param name="strName"></param>
  236. /// <returns></returns>
  237. public static decimal GetQueryDecimal(string strName)
  238. {
  239. string QueryValue = HttpContext.Current.Request.QueryString[strName];
  240. if (string.IsNullOrEmpty(QueryValue))
  241. {
  242. return -1;
  243. }
  244. else
  245. {
  246. try
  247. {
  248. return Convert.ToDecimal(QueryValue);
  249. }
  250. catch
  251. {
  252. return -1;
  253. }
  254. }
  255. }
  256. /// <summary>
  257. /// 获得当前页面的名称
  258. /// </summary>
  259. /// <returns>当前页面的名称</returns>
  260. public static string GetPageName()
  261. {
  262. string [] urlArr = HttpContext.Current.Request.Url.AbsolutePath.Split('/');
  263. return urlArr[urlArr.Length - 1].ToLower();
  264. }
  265. /// <summary>
  266. /// 返回表单或Url参数的总个数
  267. /// </summary>
  268. /// <returns></returns>
  269. public static int GetParamCount()
  270. {
  271. return HttpContext.Current.Request.Form.Count + HttpContext.Current.Request.QueryString.Count;
  272. }
  273. /// <summary>
  274. /// 获得指定表单参数的值
  275. /// </summary>
  276. /// <param name="strName">表单参数</param>
  277. /// <returns>表单参数的值</returns>
  278. public static string GetFormString(string strName)
  279. {
  280. return HttpContext.Current.Request.Form[strName];
  281. }
  282. /// <summary>
  283. /// 获得指定表单参数的数字值如果为空这返回-1
  284. /// </summary>
  285. /// <param name="strName"></param>
  286. /// <returns></returns>
  287. public static int GetFormNumber(string strName)
  288. {
  289. string FormNumber = HttpContext.Current.Request.Form[strName];
  290. if (string.IsNullOrEmpty(FormNumber))
  291. {
  292. return -1;
  293. }
  294. else
  295. {
  296. try
  297. {
  298. return Convert.ToInt32(FormNumber);
  299. }
  300. catch
  301. {
  302. return -1;
  303. }
  304. }
  305. }
  306. /// <summary>
  307. /// 获得指定表单参数的十进制值如果为空这返回-1
  308. /// </summary>
  309. /// <param name="strName"></param>
  310. /// <returns></returns>
  311. public static decimal GetFormDecimal(string strName)
  312. {
  313. string FormNumber = HttpContext.Current.Request.Form[strName];
  314. if (string.IsNullOrEmpty(FormNumber))
  315. {
  316. return -1;
  317. }
  318. else
  319. {
  320. try
  321. {
  322. return Convert.ToDecimal(FormNumber);
  323. }
  324. catch
  325. {
  326. return -1;
  327. }
  328. }
  329. }
  330. /// <summary>
  331. /// 获得Url或表单参数的值, 先判断Url参数是否为空字符串, 如为True则返回表单参数的值
  332. /// </summary>
  333. /// <param name="strName">参数</param>
  334. /// <returns>Url或表单参数的值</returns>
  335. public static string GetString(string strName)
  336. {
  337. if ("".Equals(GetQueryString(strName)))
  338. {
  339. return GetFormString(strName);
  340. }
  341. else
  342. {
  343. return GetQueryString(strName);
  344. }
  345. }
  346. /// <summary>
  347. /// 获得当前页面客户端的IP
  348. /// </summary>
  349. /// <returns>当前页面客户端的IP</returns>
  350. public static string GetIP()
  351. {
  352. string result = String.Empty;
  353. result = GetServerString("HTTP_X_FORWARDED_FOR");
  354. if (string.IsNullOrEmpty(result))
  355. {
  356. result = GetServerString("REMOTE_ADDR");
  357. }
  358. if (string.IsNullOrEmpty(result))
  359. {
  360. result = HttpContext.Current.Request.UserHostAddress;
  361. }
  362. if (string.IsNullOrEmpty(result) || !RegexUtil.IsIP(result))
  363. {
  364. return "127.0.0.1";
  365. }
  366. return result;
  367. }
  368. }
  369. }