Request
1
using
System;
2 using System.Web;
3 using System.Text;
4 using System.Text.RegularExpressions;
5
6 namespace ChinaValue.CommonV2008
7 {
8 /**//// <summary>
9 /// Request操作类
10 /// </summary>
11 public class CVRequest
12 {
13 /**//// <summary>
14 /// 判断当前页面是否接收到了Post请求
15 /// </summary>
16 /// <returns>是否接收到了Post请求</returns>
17 public static Boolean IsPost()
18 {
19 return HttpContext.Current.Request.HttpMethod.Equals("POST");
20 }
21
22 /**//// <summary>
23 /// 判断当前页面是否接收到了Get请求
24 /// </summary>
25 /// <returns>是否接收到了Get请求</returns>
26 public static Boolean IsGet()
27 {
28 return HttpContext.Current.Request.HttpMethod.Equals("GET");
29 }
30
31 /**//// <summary>
32 /// 返回指定的服务器变量信息
33 /// </summary>
34 /// <param name="strName">服务器变量名</param>
35 /// <returns>服务器变量信息</returns>
36 public static String GetServerString(string strName)
37 {
38 if (HttpContext.Current.Request.ServerVariables[strName] == null)
39 {
40 return String.Empty;
41 }
42
43 return HttpContext.Current.Request.ServerVariables[strName].ToString();
44 }
45
46 /**//// <summary>
47 /// 返回上一个页面的地址
48 /// </summary>
49 /// <returns>上一个页面的地址</returns>
50 public static String GetUrlReferrer()
51 {
52 String retVal = null;
53
54 try
55 {
56 retVal = HttpContext.Current.Request.UrlReferrer.ToString();
57 }
58 catch { }
59
60 if (retVal == null)
61 return String.Empty;
62
63 return retVal;
64
65 }
66
67 /**//// <summary>
68 /// 得到当前完整主机头
69 /// </summary>
70 /// <returns></returns>
71 public static String GetCurrentFullHost()
72 {
73 HttpRequest request = System.Web.HttpContext.Current.Request;
74
75 if (!request.Url.IsDefaultPort)
76 {
77 return string.Format("{0}:{1}", request.Url.Host, request.Url.Port.ToString());
78 }
79
80 return request.Url.Host;
81 }
82
83 /**//// <summary>
84 /// 得到主机头
85 /// </summary>
86 /// <returns></returns>
87 public static String GetHost()
88 {
89 return HttpContext.Current.Request.Url.Host;
90 }
91
92 /**//// <summary>
93 /// 获取当前请求的原始 URL(URL 中域信息之后的部分,包括查询字符串(如果存在))
94 /// </summary>
95 /// <returns>原始 URL</returns>
96 public static String GetRawUrl()
97 {
98 return HttpContext.Current.Request.RawUrl;
99 }
100
101 /**//// <summary>
102 /// 判断当前访问是否来自浏览器软件
103 /// </summary>
104 /// <returns>当前访问是否来自浏览器软件</returns>
105 public static Boolean IsBrowserGet()
106 {
107 String[] BrowserName = { "ie", "opera", "netscape", "mozilla" };
108 String curBrowser = HttpContext.Current.Request.Browser.Type.ToLower();
109
110 for (Int32 i = 0; i < BrowserName.Length; i++)
111 {
112 if (curBrowser.IndexOf(BrowserName[i]) >= 0)
113 {
114 return true;
115 }
116 }
117
118 return false;
119 }
120
121 /**//// <summary>
122 /// 判断是否来自搜索引擎链接
123 /// </summary>
124 /// <returns>是否来自搜索引擎链接</returns>
125 public static Boolean IsSearchEnginesGet()
126 {
127 String[] SearchEngine = { "google", "yahoo", "msn", "baidu", "sogou", "sohu", "sina", "163", "lycos", "tom" };
128 String tmpReferrer = HttpContext.Current.Request.UrlReferrer.ToString().ToLower();
129
130 for (int i = 0; i < SearchEngine.Length; i++)
131 {
132 if (tmpReferrer.IndexOf(SearchEngine[i]) >= 0)
133 {
134 return true;
135 }
136 }
137
138 return false;
139 }
140
141 /**//// <summary>
142 /// 获得当前完整Url地址
143 /// </summary>
144 /// <returns>当前完整Url地址</returns>
145 public static String GetUrl()
146 {
147 return HttpContext.Current.Request.Url.ToString();
148 }
149
150 /**//// <summary>
151 /// 获得指定Url参数的值(过滤SQL注入字符)
152 /// </summary>
153 /// <param name="strName">Url参数</param>
154 /// <returns>Url参数的值</returns>
155 public static String GetQueryString(String strName)
156 {
157 return GetQueryString(strName, true);
158 }
159
160 /**//// <summary>
161 /// 获得指定Url参数的值
162 /// </summary>
163 /// <param name="strName">Url参数</param>
164 /// <param name="dropInjWords">是否过滤SQL注入字符</param>
165 /// <returns>Url参数的值</returns>
166 public static String GetQueryString(String strName, Boolean dropInjWords)
167 {
168 if (HttpContext.Current.Request.QueryString[strName] == null)
169 {
170 return String.Empty;
171 }
172
173 String queryStr = HttpContext.Current.Request.QueryString[strName];
174
175 if (dropInjWords)
176 {
177 return DropInjectionWords(queryStr);
178 }
179
180 return queryStr;
181 }
182
183 /**//// <summary>
184 /// 过滤SQL注入的字符
185 /// </summary>
186 /// <param name="str"></param>
187 /// <returns></returns>
188 public static String DropInjectionWords(String str)
189 {
190 StringBuilder sb = new StringBuilder(str);
191
192 sb = sb.Replace(";", ";");
193 sb = sb.Replace("'", "’");
194
195 if (Regex.IsMatch(sb.ToString(), @"\s*select\s+"))
196 {
197 sb = sb.Replace("select", "select");
198 }
199
200 if (Regex.IsMatch(sb.ToString(), @"\s*and\s+"))
201 {
202 sb = sb.Replace("and", "and");
203 }
204 //sb = sb.Replace("=", "=");
205
206 return sb.ToString();
207 }
208
209 /**//// <summary>
210 /// 获得当前页面的名称
211 /// </summary>
212 /// <returns>当前页面的名称</returns>
213 public static String GetPageName()
214 {
215 string[] urlArr = HttpContext.Current.Request.Url.AbsolutePath.Split('/');
216 return urlArr[urlArr.Length - 1].ToLower();
217 }
218
219 /**//// <summary>
220 /// 返回表单或Url参数的总个数
221 /// </summary>
222 /// <returns></returns>
223 public static Int32 GetParamCount()
224 {
225 return HttpContext.Current.Request.Form.Count + HttpContext.Current.Request.QueryString.Count;
226 }
227
228 /**//// <summary>
229 /// 获得指定表单参数的值(过滤SQL注入字符)
230 /// </summary>
231 /// <param name="strName">表单参数</param>
232 /// <returns>表单参数的值</returns>
233 public static String GetFormString(String strName)
234 {
235 return GetFormString(strName, true);
236 }
237
238 /**//// <summary>
239 /// 获得指定表单参数的值
240 /// </summary>
241 /// <param name="strName">表单参数</param>
242 /// <param name="dropInjWords">是否过滤SQL注入字符</param>
243 /// <returns>表单参数的值</returns>
244 public static String GetFormString(String strName, Boolean dropInjWords)
245 {
246 if (HttpContext.Current.Request.Form[strName] == null)
247 {
248 return String.Empty;
249 }
250
251 String fromStr = HttpContext.Current.Request.Form[strName];
252
253 if (dropInjWords)
254 {
255 return DropInjectionWords(fromStr);
256 }
257
258 return fromStr;
259 }
260
261 /**//// <summary>
262 /// 获得Url或表单参数的值, 先判断Url参数是否为空字符串, 如为True则返回表单参数的值(过滤SQL注入字符)
263 /// </summary>
264 /// <param name="strName">参数</param>
265 /// <returns>Url或表单参数的值</returns>
266 public static String GetString(String strName)
267 {
268 if ("".Equals(GetQueryString(strName)))
269 {
270 return GetFormString(strName);
271 }
272 else
273 {
274 return GetQueryString(strName);
275 }
276 }
277
278 /**//// <summary>
279 /// 获得Url或表单参数的值, 先判断Url参数是否为空字符串, 如为True则返回表单参数的值
280 /// </summary>
281 /// <param name="strName">参数</param>
282 /// <param name="dropInjWords">是否过滤SQL注入字符</param>
283 /// <returns>Url或表单参数的值</returns>
284 public static String GetString(String strName, Boolean dropInjWords)
285 {
286 if ("".Equals(GetQueryString(strName)))
287 {
288 return GetFormString(strName, dropInjWords);
289 }
290 else
291 {
292 return GetQueryString(strName, dropInjWords);
293 }
294 }
295
296 /**//// <summary>
297 /// 获得指定Url参数的int类型值
298 /// </summary>
299 /// <param name="strName">Url参数</param>
300 /// <param name="defValue">缺省值</param>
301 /// <returns>Url参数的int类型值</returns>
302 public static Int32 GetQueryInt(String strName, Int32 defValue)
303 {
304 return ConvertStr.ToInt32(HttpContext.Current.Request.QueryString[strName], defValue);
305 }
306
307 /**//// <summary>
308 /// 获得指定表单参数的int类型值
309 /// </summary>
310 /// <param name="strName">表单参数</param>
311 /// <param name="defValue">缺省值</param>
312 /// <returns>表单参数的int类型值</returns>
313 public static Int32 GetFormInt(string strName, int defValue)
314 {
315 return ConvertStr.ToInt32(HttpContext.Current.Request.Form[strName], defValue);
316 }
317
318 /**//// <summary>
319 /// 获得指定Url或表单参数的int类型值, 先判断Url参数是否为缺省值, 如为True则返回表单参数的值
320 /// </summary>
321 /// <param name="strName">Url或表单参数</param>
322 /// <param name="defValue">缺省值</param>
323 /// <returns>Url或表单参数的int类型值</returns>
324 public static Int32 GetInt(string strName, int defValue)
325 {
326 if (GetQueryInt(strName, defValue) == defValue)
327 {
328 return GetFormInt(strName, defValue);
329 }
330 else
331 {
332 return GetQueryInt(strName, defValue);
333 }
334 }
335
336 /**//// <summary>
337 /// 获得指定Url参数的float类型值
338 /// </summary>
339 /// <param name="strName">Url参数</param>
340 /// <param name="defValue">缺省值</param>
341 /// <returns>Url参数的int类型值</returns>
342 public static float GetQueryFloat(string strName, float defValue)
343 {
344 return ConvertStr.ToFloat(HttpContext.Current.Request.QueryString[strName], defValue);
345 }
346
347
348 /**//// <summary>
349 /// 获得指定表单参数的float类型值
350 /// </summary>
351 /// <param name="strName">表单参数</param>
352 /// <param name="defValue">缺省值</param>
353 /// <returns>表单参数的float类型值</returns>
354 public static float GetFormFloat(string strName, float defValue)
355 {
356 return ConvertStr.ToFloat(HttpContext.Current.Request.Form[strName], defValue);
357 }
358
359 /**//// <summary>
360 /// 获得指定Url或表单参数的float类型值, 先判断Url参数是否为缺省值, 如为True则返回表单参数的值
361 /// </summary>
362 /// <param name="strName">Url或表单参数</param>
363 /// <param name="defValue">缺省值</param>
364 /// <returns>Url或表单参数的int类型值</returns>
365 public static float GetFloat(string strName, float defValue)
366 {
367 if (GetQueryFloat(strName, defValue) == defValue)
368 {
369 return GetFormFloat(strName, defValue);
370 }
371 else
372 {
373 return GetQueryFloat(strName, defValue);
374 }
375 }
376
377 /**//// <summary>
378 /// 获得当前页面客户端的IP
379 /// </summary>
380 /// <returns>当前页面客户端的IP</returns>
381 public static String GetIP()
382 {
383 String result = String.Empty;
384
385 result = HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
386
387 if (null == result || result == String.Empty)
388 {
389 result = HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"];
390 }
391
392 if (null == result || result == String.Empty)
393 {
394 result = HttpContext.Current.Request.UserHostAddress;
395 }
396
397 if (null == result || result == String.Empty)
398 {
399 return "0.0.0.0";
400 }
401
402 return result;
403 }
404 }
405}
2 using System.Web;
3 using System.Text;
4 using System.Text.RegularExpressions;
5
6 namespace ChinaValue.CommonV2008
7 {
8 /**//// <summary>
9 /// Request操作类
10 /// </summary>
11 public class CVRequest
12 {
13 /**//// <summary>
14 /// 判断当前页面是否接收到了Post请求
15 /// </summary>
16 /// <returns>是否接收到了Post请求</returns>
17 public static Boolean IsPost()
18 {
19 return HttpContext.Current.Request.HttpMethod.Equals("POST");
20 }
21
22 /**//// <summary>
23 /// 判断当前页面是否接收到了Get请求
24 /// </summary>
25 /// <returns>是否接收到了Get请求</returns>
26 public static Boolean IsGet()
27 {
28 return HttpContext.Current.Request.HttpMethod.Equals("GET");
29 }
30
31 /**//// <summary>
32 /// 返回指定的服务器变量信息
33 /// </summary>
34 /// <param name="strName">服务器变量名</param>
35 /// <returns>服务器变量信息</returns>
36 public static String GetServerString(string strName)
37 {
38 if (HttpContext.Current.Request.ServerVariables[strName] == null)
39 {
40 return String.Empty;
41 }
42
43 return HttpContext.Current.Request.ServerVariables[strName].ToString();
44 }
45
46 /**//// <summary>
47 /// 返回上一个页面的地址
48 /// </summary>
49 /// <returns>上一个页面的地址</returns>
50 public static String GetUrlReferrer()
51 {
52 String retVal = null;
53
54 try
55 {
56 retVal = HttpContext.Current.Request.UrlReferrer.ToString();
57 }
58 catch { }
59
60 if (retVal == null)
61 return String.Empty;
62
63 return retVal;
64
65 }
66
67 /**//// <summary>
68 /// 得到当前完整主机头
69 /// </summary>
70 /// <returns></returns>
71 public static String GetCurrentFullHost()
72 {
73 HttpRequest request = System.Web.HttpContext.Current.Request;
74
75 if (!request.Url.IsDefaultPort)
76 {
77 return string.Format("{0}:{1}", request.Url.Host, request.Url.Port.ToString());
78 }
79
80 return request.Url.Host;
81 }
82
83 /**//// <summary>
84 /// 得到主机头
85 /// </summary>
86 /// <returns></returns>
87 public static String GetHost()
88 {
89 return HttpContext.Current.Request.Url.Host;
90 }
91
92 /**//// <summary>
93 /// 获取当前请求的原始 URL(URL 中域信息之后的部分,包括查询字符串(如果存在))
94 /// </summary>
95 /// <returns>原始 URL</returns>
96 public static String GetRawUrl()
97 {
98 return HttpContext.Current.Request.RawUrl;
99 }
100
101 /**//// <summary>
102 /// 判断当前访问是否来自浏览器软件
103 /// </summary>
104 /// <returns>当前访问是否来自浏览器软件</returns>
105 public static Boolean IsBrowserGet()
106 {
107 String[] BrowserName = { "ie", "opera", "netscape", "mozilla" };
108 String curBrowser = HttpContext.Current.Request.Browser.Type.ToLower();
109
110 for (Int32 i = 0; i < BrowserName.Length; i++)
111 {
112 if (curBrowser.IndexOf(BrowserName[i]) >= 0)
113 {
114 return true;
115 }
116 }
117
118 return false;
119 }
120
121 /**//// <summary>
122 /// 判断是否来自搜索引擎链接
123 /// </summary>
124 /// <returns>是否来自搜索引擎链接</returns>
125 public static Boolean IsSearchEnginesGet()
126 {
127 String[] SearchEngine = { "google", "yahoo", "msn", "baidu", "sogou", "sohu", "sina", "163", "lycos", "tom" };
128 String tmpReferrer = HttpContext.Current.Request.UrlReferrer.ToString().ToLower();
129
130 for (int i = 0; i < SearchEngine.Length; i++)
131 {
132 if (tmpReferrer.IndexOf(SearchEngine[i]) >= 0)
133 {
134 return true;
135 }
136 }
137
138 return false;
139 }
140
141 /**//// <summary>
142 /// 获得当前完整Url地址
143 /// </summary>
144 /// <returns>当前完整Url地址</returns>
145 public static String GetUrl()
146 {
147 return HttpContext.Current.Request.Url.ToString();
148 }
149
150 /**//// <summary>
151 /// 获得指定Url参数的值(过滤SQL注入字符)
152 /// </summary>
153 /// <param name="strName">Url参数</param>
154 /// <returns>Url参数的值</returns>
155 public static String GetQueryString(String strName)
156 {
157 return GetQueryString(strName, true);
158 }
159
160 /**//// <summary>
161 /// 获得指定Url参数的值
162 /// </summary>
163 /// <param name="strName">Url参数</param>
164 /// <param name="dropInjWords">是否过滤SQL注入字符</param>
165 /// <returns>Url参数的值</returns>
166 public static String GetQueryString(String strName, Boolean dropInjWords)
167 {
168 if (HttpContext.Current.Request.QueryString[strName] == null)
169 {
170 return String.Empty;
171 }
172
173 String queryStr = HttpContext.Current.Request.QueryString[strName];
174
175 if (dropInjWords)
176 {
177 return DropInjectionWords(queryStr);
178 }
179
180 return queryStr;
181 }
182
183 /**//// <summary>
184 /// 过滤SQL注入的字符
185 /// </summary>
186 /// <param name="str"></param>
187 /// <returns></returns>
188 public static String DropInjectionWords(String str)
189 {
190 StringBuilder sb = new StringBuilder(str);
191
192 sb = sb.Replace(";", ";");
193 sb = sb.Replace("'", "’");
194
195 if (Regex.IsMatch(sb.ToString(), @"\s*select\s+"))
196 {
197 sb = sb.Replace("select", "select");
198 }
199
200 if (Regex.IsMatch(sb.ToString(), @"\s*and\s+"))
201 {
202 sb = sb.Replace("and", "and");
203 }
204 //sb = sb.Replace("=", "=");
205
206 return sb.ToString();
207 }
208
209 /**//// <summary>
210 /// 获得当前页面的名称
211 /// </summary>
212 /// <returns>当前页面的名称</returns>
213 public static String GetPageName()
214 {
215 string[] urlArr = HttpContext.Current.Request.Url.AbsolutePath.Split('/');
216 return urlArr[urlArr.Length - 1].ToLower();
217 }
218
219 /**//// <summary>
220 /// 返回表单或Url参数的总个数
221 /// </summary>
222 /// <returns></returns>
223 public static Int32 GetParamCount()
224 {
225 return HttpContext.Current.Request.Form.Count + HttpContext.Current.Request.QueryString.Count;
226 }
227
228 /**//// <summary>
229 /// 获得指定表单参数的值(过滤SQL注入字符)
230 /// </summary>
231 /// <param name="strName">表单参数</param>
232 /// <returns>表单参数的值</returns>
233 public static String GetFormString(String strName)
234 {
235 return GetFormString(strName, true);
236 }
237
238 /**//// <summary>
239 /// 获得指定表单参数的值
240 /// </summary>
241 /// <param name="strName">表单参数</param>
242 /// <param name="dropInjWords">是否过滤SQL注入字符</param>
243 /// <returns>表单参数的值</returns>
244 public static String GetFormString(String strName, Boolean dropInjWords)
245 {
246 if (HttpContext.Current.Request.Form[strName] == null)
247 {
248 return String.Empty;
249 }
250
251 String fromStr = HttpContext.Current.Request.Form[strName];
252
253 if (dropInjWords)
254 {
255 return DropInjectionWords(fromStr);
256 }
257
258 return fromStr;
259 }
260
261 /**//// <summary>
262 /// 获得Url或表单参数的值, 先判断Url参数是否为空字符串, 如为True则返回表单参数的值(过滤SQL注入字符)
263 /// </summary>
264 /// <param name="strName">参数</param>
265 /// <returns>Url或表单参数的值</returns>
266 public static String GetString(String strName)
267 {
268 if ("".Equals(GetQueryString(strName)))
269 {
270 return GetFormString(strName);
271 }
272 else
273 {
274 return GetQueryString(strName);
275 }
276 }
277
278 /**//// <summary>
279 /// 获得Url或表单参数的值, 先判断Url参数是否为空字符串, 如为True则返回表单参数的值
280 /// </summary>
281 /// <param name="strName">参数</param>
282 /// <param name="dropInjWords">是否过滤SQL注入字符</param>
283 /// <returns>Url或表单参数的值</returns>
284 public static String GetString(String strName, Boolean dropInjWords)
285 {
286 if ("".Equals(GetQueryString(strName)))
287 {
288 return GetFormString(strName, dropInjWords);
289 }
290 else
291 {
292 return GetQueryString(strName, dropInjWords);
293 }
294 }
295
296 /**//// <summary>
297 /// 获得指定Url参数的int类型值
298 /// </summary>
299 /// <param name="strName">Url参数</param>
300 /// <param name="defValue">缺省值</param>
301 /// <returns>Url参数的int类型值</returns>
302 public static Int32 GetQueryInt(String strName, Int32 defValue)
303 {
304 return ConvertStr.ToInt32(HttpContext.Current.Request.QueryString[strName], defValue);
305 }
306
307 /**//// <summary>
308 /// 获得指定表单参数的int类型值
309 /// </summary>
310 /// <param name="strName">表单参数</param>
311 /// <param name="defValue">缺省值</param>
312 /// <returns>表单参数的int类型值</returns>
313 public static Int32 GetFormInt(string strName, int defValue)
314 {
315 return ConvertStr.ToInt32(HttpContext.Current.Request.Form[strName], defValue);
316 }
317
318 /**//// <summary>
319 /// 获得指定Url或表单参数的int类型值, 先判断Url参数是否为缺省值, 如为True则返回表单参数的值
320 /// </summary>
321 /// <param name="strName">Url或表单参数</param>
322 /// <param name="defValue">缺省值</param>
323 /// <returns>Url或表单参数的int类型值</returns>
324 public static Int32 GetInt(string strName, int defValue)
325 {
326 if (GetQueryInt(strName, defValue) == defValue)
327 {
328 return GetFormInt(strName, defValue);
329 }
330 else
331 {
332 return GetQueryInt(strName, defValue);
333 }
334 }
335
336 /**//// <summary>
337 /// 获得指定Url参数的float类型值
338 /// </summary>
339 /// <param name="strName">Url参数</param>
340 /// <param name="defValue">缺省值</param>
341 /// <returns>Url参数的int类型值</returns>
342 public static float GetQueryFloat(string strName, float defValue)
343 {
344 return ConvertStr.ToFloat(HttpContext.Current.Request.QueryString[strName], defValue);
345 }
346
347
348 /**//// <summary>
349 /// 获得指定表单参数的float类型值
350 /// </summary>
351 /// <param name="strName">表单参数</param>
352 /// <param name="defValue">缺省值</param>
353 /// <returns>表单参数的float类型值</returns>
354 public static float GetFormFloat(string strName, float defValue)
355 {
356 return ConvertStr.ToFloat(HttpContext.Current.Request.Form[strName], defValue);
357 }
358
359 /**//// <summary>
360 /// 获得指定Url或表单参数的float类型值, 先判断Url参数是否为缺省值, 如为True则返回表单参数的值
361 /// </summary>
362 /// <param name="strName">Url或表单参数</param>
363 /// <param name="defValue">缺省值</param>
364 /// <returns>Url或表单参数的int类型值</returns>
365 public static float GetFloat(string strName, float defValue)
366 {
367 if (GetQueryFloat(strName, defValue) == defValue)
368 {
369 return GetFormFloat(strName, defValue);
370 }
371 else
372 {
373 return GetQueryFloat(strName, defValue);
374 }
375 }
376
377 /**//// <summary>
378 /// 获得当前页面客户端的IP
379 /// </summary>
380 /// <returns>当前页面客户端的IP</returns>
381 public static String GetIP()
382 {
383 String result = String.Empty;
384
385 result = HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
386
387 if (null == result || result == String.Empty)
388 {
389 result = HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"];
390 }
391
392 if (null == result || result == String.Empty)
393 {
394 result = HttpContext.Current.Request.UserHostAddress;
395 }
396
397 if (null == result || result == String.Empty)
398 {
399 return "0.0.0.0";
400 }
401
402 return result;
403 }
404 }
405}