| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273 |
- using System;
- using System.Web;
- namespace Common
- {
- public class RequestBase
- {
- public static bool IsPost()
- {
- return HttpContext.Current.Request.HttpMethod.Equals("POST");
- }
- public static bool IsGet()
- {
- return HttpContext.Current.Request.HttpMethod.Equals("GET");
- }
- public static string GetServerString(string strName)
- {
- string result;
- if (HttpContext.Current.Request.ServerVariables[strName] == null)
- {
- result = "";
- }
- else
- {
- result = HttpContext.Current.Request.ServerVariables[strName].ToString();
- }
- return result;
- }
- public static string GetUrlReferrer()
- {
- string retVal = null;
- try
- {
- retVal = HttpContext.Current.Request.UrlReferrer.ToString();
- }
- catch
- {
- }
- string result;
- if (retVal == null)
- {
- result = "";
- }
- else
- {
- result = retVal;
- }
- return result;
- }
- public static string GetCurrentFullHost()
- {
- HttpRequest request = HttpContext.Current.Request;
- string result;
- if (!request.Url.IsDefaultPort)
- {
- result = string.Format("{0}:{1}", request.Url.Host, request.Url.Port.ToString());
- }
- else
- {
- result = request.Url.Host;
- }
- return result;
- }
- public static string GetHost()
- {
- return HttpContext.Current.Request.Url.Host;
- }
- public static string GetRawUrl()
- {
- return HttpContext.Current.Request.RawUrl;
- }
- public static bool IsBrowserGet()
- {
- string[] BrowserName = new string[]
- {
- "ie",
- "opera",
- "netscape",
- "mozilla"
- };
- string curBrowser = HttpContext.Current.Request.Browser.Type.ToLower();
- bool result;
- for (int i = 0; i < BrowserName.Length; i++)
- {
- if (curBrowser.IndexOf(BrowserName[i]) >= 0)
- {
- result = true;
- return result;
- }
- }
- result = false;
- return result;
- }
- public static bool IsSearchEnginesGet()
- {
- string[] SearchEngine = new string[]
- {
- "google",
- "yahoo",
- "msn",
- "baidu",
- "sogou",
- "sohu",
- "sina",
- "163",
- "lycos",
- "tom"
- };
- string tmpReferrer = HttpContext.Current.Request.UrlReferrer.ToString().ToLower();
- bool result;
- for (int i = 0; i < SearchEngine.Length; i++)
- {
- if (tmpReferrer.IndexOf(SearchEngine[i]) >= 0)
- {
- result = true;
- return result;
- }
- }
- result = false;
- return result;
- }
- public static string GetUrl()
- {
- return HttpContext.Current.Request.Url.ToString();
- }
- public static string GetQueryString(string strName)
- {
- string result;
- if (HttpContext.Current.Request.QueryString[strName] == null)
- {
- result = "";
- }
- else
- {
- result = HttpContext.Current.Request.QueryString[strName];
- }
- return result;
- }
- public static string GetPageName()
- {
- string[] urlArr = HttpContext.Current.Request.Url.AbsolutePath.Split(new char[]
- {
- '/'
- });
- return urlArr[urlArr.Length - 1].ToLower();
- }
- public static int GetParamCount()
- {
- return HttpContext.Current.Request.Form.Count + HttpContext.Current.Request.QueryString.Count;
- }
- public static string GetFormString(string strName)
- {
- string result;
- if (HttpContext.Current.Request.Form[strName] == null)
- {
- result = "";
- }
- else
- {
- result = HttpContext.Current.Request.Form[strName];
- }
- return result;
- }
- public static string GetString(string strName)
- {
- string result;
- if ("".Equals(RequestBase.GetQueryString(strName)))
- {
- result = RequestBase.GetFormString(strName);
- }
- else
- {
- result = RequestBase.GetQueryString(strName);
- }
- return result;
- }
- public static int GetQueryInt(string strName, int defValue)
- {
- return Utils.StrToInt(HttpContext.Current.Request.QueryString[strName], defValue);
- }
- public static int GetFormInt(string strName, int defValue)
- {
- return Utils.StrToInt(HttpContext.Current.Request.Form[strName], defValue);
- }
- public static int GetInt(string strName, int defValue)
- {
- int result;
- if (RequestBase.GetQueryInt(strName, defValue) == defValue)
- {
- result = RequestBase.GetFormInt(strName, defValue);
- }
- else
- {
- result = RequestBase.GetQueryInt(strName, defValue);
- }
- return result;
- }
- public static float GetQueryFloat(string strName, float defValue)
- {
- return Utils.StrToFloat(HttpContext.Current.Request.QueryString[strName], defValue);
- }
- public static float GetFormFloat(string strName, float defValue)
- {
- return Utils.StrToFloat(HttpContext.Current.Request.Form[strName], defValue);
- }
- public static float GetFloat(string strName, float defValue)
- {
- float result;
- if (RequestBase.GetQueryFloat(strName, defValue) == defValue)
- {
- result = RequestBase.GetFormFloat(strName, defValue);
- }
- else
- {
- result = RequestBase.GetQueryFloat(strName, defValue);
- }
- return result;
- }
- public static string GetIP()
- {
- string result = string.Empty;
- result = HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
- if (result == null || result == string.Empty)
- {
- result = HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"];
- }
- if (result == null || result == string.Empty)
- {
- result = HttpContext.Current.Request.UserHostAddress;
- }
- string result2;
- if (result == null || result == string.Empty || !Utils.IsIP(result))
- {
- result2 = "0.0.0.0";
- }
- else
- {
- result2 = result;
- }
- return result2;
- }
- public static void SaveRequestFile(string path)
- {
- if (HttpContext.Current.Request.Files.Count > 0)
- {
- HttpContext.Current.Request.Files[0].SaveAs(path);
- }
- }
- }
- }
|