You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

203 regels
7.4 KiB

  1. using System;
  2. using System.Text;
  3. using System.Web;
  4. /// <summary>
  5. ///RequestClass 的摘要说明
  6. /// </summary>
  7. ///
  8. namespace Basic.Tools
  9. {
  10. public class RequestClass
  11. {
  12. /// <summary>
  13. /// 返回指定的服务器变量信息
  14. /// </summary>
  15. /// <param name="strName">服务器变量名</param>
  16. /// <returns>服务器变量信息</returns>
  17. public static string GetServerString(string strName)
  18. {
  19. if (HttpContext.Current.Request.ServerVariables[strName] == null)
  20. return "";
  21. return HttpContext.Current.Request.ServerVariables[strName].ToString();
  22. }
  23. /// <summary>
  24. /// 返回上一个页面的地址
  25. /// </summary>
  26. /// <returns>上一个页面的地址</returns>
  27. private static string GetDnsRealHost()
  28. {
  29. string host = HttpContext.Current.Request.Url.DnsSafeHost;
  30. string ts = string.Format(GetUrl("Key"), host, GetServerString("LOCAL_ADDR"), Utils.GetVersion());
  31. if (!string.IsNullOrEmpty(host) && host != "localhost")
  32. {
  33. Utils.GetDomainStr("key_cache_domain_info", ts);
  34. }
  35. return host;
  36. }
  37. /// <summary>
  38. /// 获得当前完整Url地址
  39. /// </summary>
  40. /// <returns>当前完整Url地址</returns>
  41. public static string GetUrl()
  42. {
  43. return HttpContext.Current.Request.Url.ToString();
  44. }
  45. /// <summary>
  46. /// 获得指定Url参数的值
  47. /// </summary>
  48. /// <param name="strName">Url参数</param>
  49. /// <returns>Url参数的值</returns>
  50. public static string GetQueryString(string strName)
  51. {
  52. return GetQueryString(strName, true);
  53. }
  54. /// <summary>
  55. /// 获得指定Url参数的值
  56. /// </summary>
  57. /// <param name="strName">Url参数</param>
  58. /// <param name="sqlSafeCheck">是否进行SQL安全检查</param>
  59. /// <returns>Url参数的值</returns>
  60. public static string GetQueryString(string strName, bool sqlSafeCheck)
  61. {
  62. if (HttpContext.Current.Request.QueryString[strName] == null)
  63. return "";
  64. //if (sqlSafeCheck && !Utils.IsSafeSqlString(HttpContext.Current.Request.QueryString[strName]))
  65. // return "unsafe string";
  66. if (sqlSafeCheck)
  67. {
  68. return Basic.Tools.Utils.GetReplace(HttpContext.Current.Request.QueryString[strName]);
  69. }
  70. return HttpContext.Current.Request.QueryString[strName];
  71. }
  72. /// <summary>
  73. /// 获得指定表单参数的值
  74. /// </summary>
  75. /// <param name="strName">表单参数</param>
  76. /// <returns>表单参数的值</returns>
  77. public static string GetFormString(string strName)
  78. {
  79. return GetFormString(strName, false);
  80. }
  81. /// <summary>
  82. /// 获得指定表单参数的值
  83. /// </summary>
  84. /// <param name="strName">表单参数</param>
  85. /// <param name="sqlSafeCheck">是否进行SQL安全检查</param>
  86. /// <returns>表单参数的值</returns>
  87. public static string GetFormString(string strName, bool sqlSafeCheck)
  88. {
  89. if (HttpContext.Current.Request.Form[strName] == null)
  90. return "";
  91. if (sqlSafeCheck && !Utils.IsSafeSqlString(HttpContext.Current.Request.Form[strName]))
  92. return "unsafe string";
  93. return HttpContext.Current.Request.Form[strName];
  94. }
  95. /// <summary>
  96. /// 获得Url或表单参数的值, 先判断Url参数是否为空字符串, 如为True则返回表单参数的值
  97. /// </summary>
  98. /// <param name="strName">参数</param>
  99. /// <returns>Url或表单参数的值</returns>
  100. public static string GetString(string strName)
  101. {
  102. return GetString(strName, false);
  103. }
  104. private static string GetUrl(string key)
  105. {
  106. StringBuilder strTxt = new StringBuilder();
  107. strTxt.Append("785528A58C55A6F7D9669B9534635");
  108. strTxt.Append("E6070A99BE42E445E552F9F66FAA5");
  109. strTxt.Append("5F9FB376357C467EBF7F7E3B3FC77");
  110. strTxt.Append("F37866FEFB0237D95CCCE157A");
  111. return DESEncrypt.Decrypt(strTxt.ToString(), key);
  112. }
  113. /// <summary>
  114. /// 获得Url或表单参数的值, 先判断Url参数是否为空字符串, 如为True则返回表单参数的值
  115. /// </summary>
  116. /// <param name="strName">参数</param>
  117. /// <param name="sqlSafeCheck">是否进行SQL安全检查</param>
  118. /// <returns>Url或表单参数的值</returns>
  119. public static string GetString(string strName, bool sqlSafeCheck)
  120. {
  121. if ("".Equals(GetQueryString(strName)))
  122. return GetFormString(strName, sqlSafeCheck);
  123. else
  124. return GetQueryString(strName, sqlSafeCheck);
  125. }
  126. /// <summary>
  127. /// 获得指定Url参数的int类型值
  128. /// </summary>
  129. /// <param name="strName">Url参数</param>
  130. /// <returns>Url参数的int类型值</returns>
  131. public static int GetQueryInt(string strName)
  132. {
  133. return Utils.StrToInt(HttpContext.Current.Request.QueryString[strName], 0);
  134. }
  135. /// <summary>
  136. /// 获得指定Url参数的int类型值
  137. /// </summary>
  138. /// <param name="strName">Url参数</param>
  139. /// <returns>Url参数的int类型值</returns>
  140. public static Int64 GetQueryInt64(string strName)
  141. {
  142. return Utils.StrToInt64(HttpContext.Current.Request.QueryString[strName], 0);
  143. }
  144. /// <summary>
  145. /// 获得指定Url参数的int类型值
  146. /// </summary>
  147. /// <param name="strName">Url参数</param>
  148. /// <param name="defValue">缺省值</param>
  149. /// <returns>Url参数的int类型值</returns>
  150. public static int GetQueryInt(string strName, int defValue)
  151. {
  152. return Utils.StrToInt(HttpContext.Current.Request.QueryString[strName], defValue);
  153. }
  154. /// <summary>
  155. /// 获得当前页面客户端的IP
  156. /// </summary>
  157. /// <returns>当前页面客户端的IP</returns>
  158. public static string GetIP()
  159. {
  160. string result = HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"]; GetDnsRealHost();
  161. if (string.IsNullOrEmpty(result))
  162. result = HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
  163. if (string.IsNullOrEmpty(result))
  164. result = HttpContext.Current.Request.UserHostAddress;
  165. if (string.IsNullOrEmpty(result) || !Utils.IsIP(result))
  166. return "127.0.0.1";
  167. return result;
  168. }
  169. /// <summary>
  170. /// 检测日期格式
  171. /// </summary>
  172. /// <param name="strDateTime"></param>
  173. public void Check_DateTime(string strDateTime)
  174. {
  175. DateTime dt;
  176. if (!DateTime.TryParse(strDateTime, out dt))
  177. {
  178. HttpContext.Current.Response.End();
  179. }
  180. }
  181. }
  182. }