/// <summary>
/// Web页面方法
/// </summary>
public class WebHelper
{
/// <summary>
/// 校验是否为内部提交数据
/// </summary>
/// <param name="rq"></param>
/// <returns>false=外部提交数据;</returns>
public static bool VerifyPost(HttpContext rq)
{
string host = "";
if (rq.Request.ServerVariables["SERVER_NAME"] != null)
{
host = rq.Request.ServerVariables["SERVER_NAME"].ToString();
}
string fromUrl = "";
if (rq.Request.UrlReferrer != null)
{
fromUrl = rq.Request.UrlReferrer.ToString();
}
if (fromUrl == "" || host == "")
{
return false;
}
else
{
host = "http://" + host.ToLower();
fromUrl = fromUrl.ToLower();
int a = fromUrl.IndexOf(host);
if (fromUrl.IndexOf(host) < 0)
{
return false;
}
}
return true;
}
/// <summary>
/// 设置页面不被缓存
/// </summary>
public static void SetPageNoCache()
{
HttpContext.Current.Response.Buffer = true;
HttpContext.Current.Response.ExpiresAbsolute = System.DateTime.Now.AddSeconds(-1);
HttpContext.Current.Response.Expires = 0;
HttpContext.Current.Response.CacheControl = "no-cache";
HttpContext.Current.Response.AddHeader("Pragma", "No-Cache");
}
/// <summary>
/// 获取查询的参数数字值
/// </summary>
/// <param name="name"></param>
/// <returns></returns>
public static int GetQueryIntValue(string name)
{
if (HttpContext.Current.Request.QueryString[name] != null)
{
try
{
return Convert.ToInt32(HttpContext.Current.Request.QueryString[name].ToString());
}
catch
{
return 0;
}
}
else return 0;
}
/// <summary>
/// 获取查询的参数字符值
/// </summary>
/// <param name="name"></param>
/// <returns></returns>
public static string GetQueryStringValue(string name)
{
if (HttpContext.Current.Request.QueryString[name] != null)
{
try
{
return HttpContext.Current.Request.QueryString[name].ToString();
}
catch
{
return string.Empty;
}
}
else return string.Empty;
}
/// <summary>
/// 获得访客IP
/// </summary>
/// <returns></returns>
public static string GetIP()
{
string IP = HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"];
if (string.IsNullOrEmpty(IP))
{
IP = HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
}
if (string.IsNullOrEmpty(IP))
{
IP = HttpContext.Current.Request.UserHostAddress;
}
return IP;
}
/// <summary>
/// 返回上一请求页面URL
/// </summary>
/// <returns></returns>
public static string GetUrlReferrer()
{
if (HttpContext.Current.Request.UrlReferrer != null)
{
return HttpContext.Current.Request.UrlReferrer.ToString();
}
return string.Empty;
}
/// <summary>
/// 判断访问来源是否来自搜索引擎
/// </summary>
/// <returns></returns>
public static bool IsSearchEnginesGet(ref string searchEnginesName)
{
if (HttpContext.Current.Request.UrlReferrer != null)
{
string[] strArray = new string[] { "google", "yahoo", "msn", "baidu", "sogou", "sohu", "sina", "163", "lycos", "tom", "yisou", "iask", "soso", "gougou", "zhongsou" };
string str = HttpContext.Current.Request.UrlReferrer.ToString().ToLower();
for (int i = 0; i < strArray.Length; i++)
{
if (str.IndexOf(strArray[i]) >= 0)
{
searchEnginesName = strArray[i];
return true;
}
}
}
return false;
}
/// <summary>
/// 移除网址中的指定查询参数信息
/// </summary>
/// <param name="url">要移除参数的网址</param>
/// <param name="removeParam">要移除的参数名称</param>
/// <returns>返回移除指定参数后的网址</returns>
public static string RemoveQueryParam(string url, string removeParam)
{
string[] queryParams = { removeParam };
return RemoveQueryParam(url, queryParams);
}
/// <summary>
/// 返回网站的根目录下的第一个文件夹名称
/// </summary>
/// <param name="url">通过Request.Url.AbsolutePath获得的路径</param>
/// <returns></returns>
public static string GetFirstFolderName(string url)
{
string[] path = url.Split('/');
return path[1];
}
/// <summary>
/// 获得主机的顶级域名
/// </summary>
/// <param name="domain">IP地址或域名</param>
/// <returns></returns>
public static string GetTopDomain(string domain)
{
string str = domain;
if (str.IndexOf(".") > 0)
{
string[] strArr = str.Split(':')[0].Split('.');
if (Validator.IsNumeric(strArr[strArr.Length - 1]))
{
return str;
}
else
{
string domainRules = "|com.cn|net.cn|org.cn|gov.cn|com.hk|com|net|org|int|edu|gov|mil|arpa|Asia|biz|info|name|pro|coop|aero|museum|ac|ad|ae|af|ag|ai|al|am|an|ao|aq|ar|as|at|au|aw|az|ba|bb|bd|be|bf|bg|bh|bi|bj|bm|bn|bo|br|bs|bt|bv|bw|by|bz|ca|cc|cf|cg|ch|ci|ck|cl|cm|cn|co|cq|cr|cu|cv|cx|cy|cz|de|dj|dk|dm|do|dz|ec|ee|eg|eh|es|et|ev|fi|fj|fk|fm|fo|fr|ga|gb|gd|ge|gf|gh|gi|gl|gm|gn|gp|gr|gt|gu|gw|gy|hk|hm|hn|hr|ht|hu|id|ie|il|in|io|iq|ir|is|it|jm|jo|jp|ke|kg|kh|ki|km|kn|kp|kr|kw|ky|kz|la|lb|lc|li|lk|lr|ls|lt|lu|lv|ly|ma|mc|md|me|mg|mh|ml|mm|mn|mo|mp|mq|mr|ms|mt|mv|mw|mx|my|mz|na|nc|ne|nf|ng|ni|nl|no|np|nr|nt|nu|nz|om|pa|pe|pf|pg|ph|pk|pl|pm|pn|pr|pt|pw|py|qa|re|ro|ru|rw|sa|sb|sc|sd|se|sg|sh|si|sj|sk|sl|sm|sn|so|sr|st|su|sy|sz|tc|td|tf|tg|th|tj|tk|tm|tn|to|tp|tr|tt|tv|tw|tz|ua|ug|uk|us|uy|va|vc|ve|vg|vn|vu|wf|ws|ye|yu|za|zm|zr|zw|";
string tempDomain;
if (strArr.Length >= 4)
{
tempDomain = strArr[strArr.Length - 3] + "." + strArr[strArr.Length - 2] + "." + strArr[strArr.Length - 1];
if (domainRules.IndexOf("|" + tempDomain + "|") > 0)
{
return strArr[strArr.Length - 4] + "." + tempDomain;
}
}
if (strArr.Length >= 3)
{
tempDomain = strArr[strArr.Length - 2] + "." + strArr[strArr.Length - 1];
if (domainRules.IndexOf("|" + tempDomain + "|") > 0)
{
return strArr[strArr.Length - 3] + "." + tempDomain;
}
}
if (strArr.Length >= 2)
{
tempDomain = strArr[strArr.Length - 1];
if (domainRules.IndexOf("|" + tempDomain + "|") > 0)
{
return strArr[strArr.Length - 2] + "." + tempDomain;
}
}
}
}
return string.Empty;
}
/// <summary>
/// 移除网址中的指定查询参数信息
/// </summary>
/// <param name="url">要移除参数的网址</param>
/// <param name="removeParam">多个要移除的查询参数名称</param>
/// <returns>返回移除指定参数后的网址</returns>
public static string RemoveQueryParam(string url, string[] removeParam)
{
string result = url;
if (url.IndexOf("?") > 0)
{
string query = url.Substring(url.IndexOf("?") + 1);
// 分解查询参数
string[] queryParams = query.Split('&');
// 将查询参数添加集合
NameValueCollection nvc = new NameValueCollection();
for (int i = 0; i < queryParams.Length; i++)
{
if (!string.IsNullOrEmpty(queryParams[i]))
{
string name = string.Empty;
string value = string.Empty;
if (queryParams[i].IndexOf("=") > 0)
{
name = queryParams[i].Substring(0, queryParams[i].IndexOf("="));
value = queryParams[i].Substring(queryParams[i].IndexOf("=") + 1);
}
else
{
name = queryParams[i];
}
if (name != string.Empty && name != "=") nvc.Add(name, value);
}
}
// 从集合中移除指定参数
for (int i = 0; i < removeParam.Length; i++)
{
nvc.Remove(removeParam[i].Trim());
}
// 重新拼凑查询参数
query = string.Empty;
for (int i = 0; i < nvc.Count; i++)
{
if (string.IsNullOrEmpty(nvc[i]))
{
query += nvc.GetKey(i);
}
else
{
query += string.Format("{0}={1}", nvc.GetKey(i), nvc[i]);
}
if (i < nvc.Count - 1) query += "&";
}
// 返回结果
result = url.Substring(0, url.IndexOf("?") + 1);
result += query;
return result;
}
else
{
// 没有查询参数,直接返回原网址
return result;
}
}
#region Cookie操作
/// <summary>
/// 获得Cookie值
/// </summary>
/// <param name="cookieName">Cookie名称</param>
/// <returns></returns>
public static string GetCookie(string cookieName)
{
if ((HttpContext.Current.Request.Cookies != null) && (HttpContext.Current.Request.Cookies[cookieName] != null))
{
return HttpContext.Current.Request.Cookies[cookieName].Value.ToString();
}
return string.Empty;
}
/// <summary>
/// 获得Cookie的键值
/// </summary>
/// <param name="cookieName">Cookie名称</param>
/// <param name="key">键值</param>
/// <returns></returns>
public static string GetCookie(string cookieName, string key)
{
if (((HttpContext.Current.Request.Cookies != null) && (HttpContext.Current.Request.Cookies[cookieName] != null)) && (HttpContext.Current.Request.Cookies[cookieName][key] != null))
{
return HttpContext.Current.Request.Cookies[cookieName][key].ToString();
}
return "";
}
public static void WriteCookie(string cookieName, string strValue)
{
HttpCookie cookie = HttpContext.Current.Request.Cookies[cookieName];
if (cookie == null)
{
cookie = new HttpCookie(cookieName);
}
cookie.Value = strValue;
HttpContext.Current.Response.AppendCookie(cookie);
}
/// <summary>
/// 写Cookie
/// </summary>
/// <param name="cookieName">Cookie名称</param>
/// <param name="strValue">Cookie值</param>
/// <param name="expires">Cookie过期时间,单位:分钟</param>
public static void WriteCookie(string cookieName, string strValue, int expires)
{
HttpCookie cookie = HttpContext.Current.Request.Cookies[cookieName];
if (cookie == null)
{
cookie = new HttpCookie(cookieName);
}
cookie.Value = strValue;
cookie.Expires = DateTime.Now.AddMinutes((double)expires);
HttpContext.Current.Response.AppendCookie(cookie);
}
/// <summary>
/// 写Cookie
/// </summary>
/// <param name="cookieName">Cookie名称</param>
/// <param name="key"></param>
/// <param name="strValue"></param>
public static void WriteCookie(string cookieName, string key, string strValue)
{
HttpCookie cookie = HttpContext.Current.Request.Cookies[cookieName];
if (cookie == null)
{
cookie = new HttpCookie(cookieName);
}
cookie[key] = strValue;
HttpContext.Current.Response.AppendCookie(cookie);
}
#endregion
}