HttpContext常用的方法
2015-12-05 09:20:51 访问(2465) 赞(0) 踩(0)
using System;
using System.Collections.Generic;
using System.Text;
using System.Reflection;
using System.IO;
using System.Web;
using SlowX.Functions.Common;
using System.Net;
namespace SlowX.Functions.Functions
{
/// <summary>
/// HttpContext常用的方法
/// </summary>
public class HttpContextSlowXFunctions
{
/// <summary>
/// 获得URL参数
/// </summary>
/// <param name="sParam"></param>
/// <returns></returns>
public static string GetQueryString(string sParam)
{
return HttpContext.Current.Request.QueryString[sParam];
}
/// <summary>
/// 获得URL参数
/// </summary>
/// <param name="sParam"></param>
/// <returns></returns>
public static string GetParameter(string sParam)
{
string theValue = HttpContext.Current.Request.QueryString[sParam];
if (theValue == null || theValue.Length == 0)
return "";
theValue = HttpContext.Current.Server.UrlDecode(theValue);
return theValue.Trim();
}
/// <summary>
/// 获得Int参数
/// </summary>
/// <param name="sParam"></param>
/// <returns></returns>
public static int GetIntParameter(string sParam)
{
string theValue = HttpContext.Current.Request.QueryString[sParam];
if (theValue == null || theValue.Length == 0)
return 0;
int theResult = 0;
if (int.TryParse(theValue, out theResult))
return theResult;
return 0;
}
/// <summary>
/// 获得Int参数
/// </summary>
/// <param name="context"></param>
/// <param name="sParam"></param>
/// <returns></returns>
public static int GetIntParameter(HttpContext context, string sParam)
{
string theValue = context.Request.QueryString[sParam];
if (theValue == null || theValue.Length == 0)
return 0;
int theResult = 0;
if (int.TryParse(theValue, out theResult))
return theResult;
return 0;
}
/// <summary>
/// 获得Int参数
/// </summary>
/// <param name="sParam"></param>
/// <param name="defaultValue"></param>
/// <returns></returns>
public static int GetIntParameter(string sParam, int defaultValue)
{
string theValue = HttpContext.Current.Request.QueryString[sParam];
if (theValue == null || theValue.Length == 0)
return defaultValue;
int theResult = 0;
if (int.TryParse(theValue, out theResult))
return theResult;
return defaultValue;
}
/// <summary>
/// 获得Int参数
/// </summary>
/// <param name="context"></param>
/// <param name="sParam"></param>
/// <param name="defaultValue"></param>
/// <returns></returns>
public static int GetIntParameter(HttpContext context, string sParam, int defaultValue)
{
string theValue = context.Request.QueryString[sParam];
if (theValue == null || theValue.Length == 0)
return defaultValue;
int theResult = 0;
if (int.TryParse(theValue, out theResult))
return theResult;
return defaultValue;
}
/// <summary>
/// 获得Long参数
/// </summary>
/// <param name="sParam"></param>
/// <returns></returns>
public static long GetLongIDParameter()
{
string theValue = HttpContext.Current.Request.QueryString["id"];
if (theValue == null || theValue.Length == 0)
return 0;
long theResult = 0;
if (long.TryParse(theValue, out theResult))
return theResult;
return 0;
}
/// <summary>
/// 获得Long参数
/// </summary>
/// <param name="context"></param>
/// <returns></returns>
public static long GetLongIDParameter(HttpContext context)
{
string theValue = context.Request.QueryString["id"];
if (theValue == null || theValue.Length == 0)
return 0;
long theResult = 0;
if (long.TryParse(theValue, out theResult))
return theResult;
return 0;
}
/// <summary>
/// 获得Long参数
/// </summary>
/// <param name="sParam"></param>
/// <returns></returns>
public static long GetLongParameter(string sParam)
{
string theValue = HttpContext.Current.Request.QueryString[sParam];
if (theValue == null || theValue.Length == 0)
return 0;
long theResult = 0;
if (long.TryParse(theValue, out theResult))
return theResult;
return 0;
}
/// <summary>
/// 获得Long参数
/// </summary>
/// <param name="context"></param>
/// <param name="sParam"></param>
/// <returns></returns>
public static long GetLongParameter(HttpContext context, string sParam)
{
string theValue = context.Request.QueryString[sParam];
if (theValue == null || theValue.Length == 0)
return 0;
long theResult = 0;
if (long.TryParse(theValue, out theResult))
return theResult;
return 0;
}
/// <summary>
/// 获得Long参数
/// </summary>
/// <param name="sParam"></param>
/// <param name="defaultValue"></param>
/// <returns></returns>
public static long GetLongParameter(string sParam, long defaultValue)
{
string theValue = HttpContext.Current.Request.QueryString[sParam];
if (theValue == null || theValue.Length == 0)
return defaultValue;
long theResult = 0;
if (long.TryParse(theValue, out theResult))
return theResult;
return defaultValue;
}
/// <summary>
/// 获得Long参数
/// </summary>
/// <param name="context"></param>
/// <param name="sParam"></param>
/// <param name="defaultValue"></param>
/// <returns></returns>
public static long GetLongParameter(HttpContext context, string sParam, long defaultValue)
{
string theValue = context.Request.QueryString[sParam];
if (theValue == null || theValue.Length == 0)
return defaultValue;
long theResult = 0;
if (long.TryParse(theValue, out theResult))
return theResult;
return defaultValue;
}
/// <summary>
/// 获得DateTime参数
/// </summary>
/// <param name="sParam"></param>
/// <returns></returns>
public static DateTime GetDateTimeParameter(string sParam)
{
string theValue = HttpContext.Current.Request.QueryString[sParam];
if (theValue == null || theValue.Length == 0)
return DateTime.MinValue;
DateTime theResult = DateTime.MinValue;
if (DateTime.TryParse(theValue, out theResult))
return theResult;
return DateTime.MinValue;
}
/// <summary>
/// 获得DateTime参数
/// </summary>
/// <param name="sParam"></param>
/// <param name="defaultValue"></param>
/// <returns></returns>
public static DateTime GetDateTimeParameter(string sParam, DateTime defaultValue)
{
string theValue = HttpContext.Current.Request.QueryString[sParam];
if (theValue == null || theValue.Length == 0)
return defaultValue;
DateTime theResult = DateTime.MinValue;
if (DateTime.TryParse(theValue, out theResult))
return theResult;
return defaultValue;
}
/// <summary>
/// 获得 Boolean 参数
/// </summary>
/// <param name="sParam"></param>
/// <returns></returns>
public static bool GetBooleanParameter(string sParam)
{
string theValue = HttpContext.Current.Request.QueryString[sParam];
if (theValue == null || theValue.Length == 0)
return false;
if (theValue == CommonValueSlowXFunctionsCommon.STR_TRUE)
return true;
else
return false;
}
/// <summary>
/// 获得 Boolean 参数
/// </summary>
/// <param name="context"></param>
/// <param name="sParam"></param>
/// <returns></returns>
public static bool GetBooleanParameter(HttpContext context,string sParam)
{
string theValue = context.Request.QueryString[sParam];
if (theValue == null || theValue.Length == 0)
return false;
if (theValue == CommonValueSlowXFunctionsCommon.STR_TRUE)
return true;
else
return false;
}
/// <summary>
/// 获得 Boolean 参数
/// </summary>
/// <param name="sParam"></param>
/// <param name="defaultValue"></param>
/// <returns></returns>
public static bool GetBooleanParameter(string sParam, bool defaultValue)
{
string theValue = HttpContext.Current.Request.QueryString[sParam];
if (theValue == null || theValue.Length == 0)
return defaultValue;
switch (theValue)
{
case CommonValueSlowXFunctionsCommon.STR_TRUE:
return true;
case CommonValueSlowXFunctionsCommon.STR_FALSE:
return false;
default:
return defaultValue;
}
}
/// <summary>
/// 获得 Boolean 参数
/// </summary>
/// <param name="context"></param>
/// <param name="sParam"></param>
/// <param name="defaultValue"></param>
/// <returns></returns>
public static bool GetBooleanParameter(HttpContext context, string sParam, bool defaultValue)
{
string theValue = context.Request.QueryString[sParam];
if (theValue == null || theValue.Length == 0)
return defaultValue;
switch (theValue)
{
case CommonValueSlowXFunctionsCommon.STR_TRUE:
return true;
case CommonValueSlowXFunctionsCommon.STR_FALSE:
return false;
default:
return defaultValue;
}
}
/// <summary>
/// 获得 Boolean 参数
/// </summary>
/// <param name="sParam"></param>
/// <param name="defaultValue"></param>
/// <returns></returns>
public static bool? GetBooleanNullParameter(string sParam)
{
string theValue = HttpContext.Current.Request.QueryString[sParam];
if (theValue == null || theValue.Length == 0)
return null;
switch (theValue)
{
case CommonValueSlowXFunctionsCommon.STR_TRUE:
return true;
case CommonValueSlowXFunctionsCommon.STR_FALSE:
return false;
default:
return null;
}
}
/// <summary>
/// 获得 Boolean 参数
/// </summary>
/// <param name="sParam"></param>
/// <param name="defaultValue"></param>
/// <returns></returns>
public static bool? GetBooleanNullParameter(string sParam, bool? defaultValue)
{
string theValue = HttpContext.Current.Request.QueryString[sParam];
if (theValue == null || theValue.Length == 0)
return null;
switch (theValue)
{
case CommonValueSlowXFunctionsCommon.STR_TRUE:
return true;
case CommonValueSlowXFunctionsCommon.STR_FALSE:
return false;
default:
return defaultValue;
}
}
/// <summary>
/// 重建URL参数
/// </summary>
/// <returns></returns>
public static string RebuildQueryString()
{
HttpRequest hr = HttpContext.Current.Request;
int iCount = hr.QueryString.Count;
if (iCount == 0)
return "";
StringBuilder theResult = new StringBuilder();
theResult.Append(hr.QueryString.Keys[0] + "=" + hr.QueryString[hr.QueryString.Keys[0]]);
for (int i = 1; i < iCount; ++i)
{
theResult.Append("&" + hr.QueryString.Keys[i] + "=" + hr.QueryString[hr.QueryString.Keys[i]]);
}
return theResult.ToString();
}
/// <summary>
/// 重建URL参数
/// </summary>
/// <param name="noContainsKey">keyOne|keyTwo|keyThree模式</param>
/// <returns></returns>
public static string RebuildQueryStringByParams
(
string noContainsKey
)
{
HttpRequest hr = HttpContext.Current.Request;
int iCount = hr.QueryString.Count;
if (iCount == 0)
return "";
string noContainsKeyText = "";
if (noContainsKey != null)
{
noContainsKey = noContainsKey.Trim();
if (noContainsKey.Length != 0)
{
noContainsKeyText =
new StringBuilder("|").Append(noContainsKey).Append("|").ToString().ToLower();
}
}
string sKey = null;
StringBuilder theResult = new StringBuilder();
if (noContainsKeyText.Length == 0)
{
sKey = hr.QueryString.Keys[0];
theResult.Append(sKey);
theResult.Append("=");
theResult.Append(hr.QueryString[sKey]);
for (int i = 1; i < iCount; ++i)
{
theResult.Append("&");
sKey = hr.QueryString.Keys[i];
theResult.Append(sKey);
theResult.Append("=");
theResult.Append(hr.QueryString[sKey]);
}
}
else
{
bool isFill = false;
StringBuilder sbKey = null;
for (int i = 0; i < iCount; ++i)
{
sKey = hr.QueryString.Keys[i];
sbKey = new StringBuilder();
sbKey.Append("|");
sbKey.Append(sKey.Trim().ToLower());
sbKey.Append("|");
if (noContainsKeyText.Contains(sbKey.ToString()))
continue;
if (isFill)
{
theResult.Append("&");
}
else
{
isFill = true;
}
theResult.Append(sKey);
theResult.Append("=");
theResult.Append(hr.QueryString[sKey]);
}
}
return theResult.ToString();
}
/// <summary>
/// 获得网站地址
/// </summary>
/// <returns></returns>
public static string GetAbsoluteUri()
{
return HttpContext.Current.Request.Url.AbsoluteUri;
}
/// <summary>
/// 获得网站地址
/// </summary>
/// <returns></returns>
public static string GetUrlReferrer()
{
if (HttpContext.Current == null)
return "";
HttpRequest hr = HttpContext.Current.Request;
if (hr.UrlReferrer == null)
return "";
return hr.UrlReferrer.AbsoluteUri;
}
/// <summary>
/// 获得网站根目录
/// </summary>
/// <returns></returns>
public static string GetWebRootUrl()
{
HttpContext hc = HttpContext.Current;
if (hc == null)
{
throw new Exception
(
"方法:"
+ MethodBase.GetCurrentMethod().ReflectedType.FullName
+ " "
+ MethodBase.GetCurrentMethod().ToString()
+ " 发生异常:HttpContext hc = HttpContext.Current值为null。"
);
}
// 虚拟目录加完整参数页面地址 //
// /SlowXWebSite/Test/WebCommon/Default.aspx?id=default.aspx&web=%cb%aa%d2%b6&dt=D%3a%5ccanoe%5cs.aspx&p=fdf%5cf%2ffds.fdsf%3ffdf //
string strPathAndQuery = hc.Request.Url.PathAndQuery;
// 完整URL地址 //
string strAbsoluteUri = hc.Request.Url.AbsoluteUri;
int idx = strAbsoluteUri.LastIndexOf(strPathAndQuery);
if (idx == -1)
{
throw new Exception
(
"方法:"
+ MethodBase.GetCurrentMethod().ReflectedType.FullName
+ " "
+ MethodBase.GetCurrentMethod().ToString()
+ " 发生异常:"
+ "int idx = strAbsoluteUri[" + strAbsoluteUri + "].LastIndexOf(strPathAndQuery[" + strPathAndQuery + "]) == -1;"
);
}
string theResult = strAbsoluteUri.Substring(0, idx);
if (hc.Request.ApplicationPath == "/")
return theResult;
else
return theResult + hc.Request.ApplicationPath;
}
/// <summary>
/// list.aspx
/// </summary>
/// <returns></returns>
public static string GetCurPageName()
{
HttpContext hc = HttpContext.Current;
if (hc == null)
{
throw new Exception
(
"方法:"
+ MethodBase.GetCurrentMethod().ReflectedType.FullName
+ " "
+ MethodBase.GetCurrentMethod().ToString()
+ " 发生异常:"
+ "HttpContext hc = HttpContext.Current为null。"
);
}
// 虚拟目录加完整参数页面地址 //
// /SlowXWebSite/Test/WebCommon/Default.aspx?id=default.aspx&web=%cb%aa%d2%b6&dt=D%3a%5ccanoe%5cs.aspx&p=fdf%5cf%2ffds.fdsf%3ffdf //
string strPathAndQuery = hc.Request.Url.PathAndQuery;
if (strPathAndQuery == null || strPathAndQuery.Length == 0)
return "";
int idx = strPathAndQuery.LastIndexOf("?");
if (idx != -1)
strPathAndQuery = strPathAndQuery.Substring(0, idx);
idx = strPathAndQuery.LastIndexOf("/");
if (idx != -1)
strPathAndQuery = strPathAndQuery.Substring(idx + 1);
return strPathAndQuery;
}
/// <summary>
/// 如果是网站目录,返回"";如果是虚拟目录(Web),返回 /Web。则图片地址可以写成 GetPhyPath()+"/images/slowx.png"
/// </summary>
/// <returns></returns>
public static string GetPhyPath()
{
HttpContext hc = HttpContext.Current;
if (hc == null)
throw new Exception("方法:" + MethodBase.GetCurrentMethod().ReflectedType.FullName + " " + MethodBase.GetCurrentMethod().ToString() + " 发生异常:" + "HttpContext hc = HttpContext.Current 为null");
if (hc.Request.ApplicationPath == "/")
return "";
return hc.Request.ApplicationPath;
}
/// <summary>
/// 获得 /Web
/// </summary>
/// <param name="hc"></param>
/// <returns></returns>
public static string GetPhyPath(HttpContext hc)
{
if (hc.Request.ApplicationPath == "/")
return "";
return hc.Request.ApplicationPath;
}
/// <summary>
/// HttpContext.Current.Request.Params.Get("__EVENTARGUMENT")
/// </summary>
/// <returns></returns>
public static string GetEventArgument()
{
if (HttpContext.Current == null || HttpContext.Current.Request == null || HttpContext.Current.Request.Params == null)
return "";
return HttpContext.Current.Request.Params.Get("__EVENTARGUMENT");
}
/// <summary>
/// HttpContext.Current.Request.Params.Get("__EVENTTARGET")
/// </summary>
/// <returns></returns>
public static string GetEventTarget()
{
if (HttpContext.Current == null || HttpContext.Current.Request == null || HttpContext.Current.Request.Params == null)
return "";
return HttpContext.Current.Request.Params.Get("__EVENTTARGET");
}
/// <summary>
/// 获得URL的参数 不带文号
/// 比如: detail.aspx?id=1&dt=1121 == id=1&dt=1121
/// </summary>
/// <returns></returns>
public static string GetUrlQueryParams()
{
if (HttpContext.Current == null || HttpContext.Current.Request == null || HttpContext.Current.Request.Params == null)
return "";
string str = HttpContext.Current.Request.Url.Query;
if (str == null || str.Length == 0)
return "";
if (str.StartsWith("?"))
return str.Substring(1);
return str;
}
/// <summary>
/// 是否本地
/// </summary>
/// <returns></returns>
public static bool IsLocalHost()
{
if (HttpContext.Current == null)
return false;
if (HttpContext.Current.Request == null)
return false;
return HttpContext.Current.Request.IsLocal;
// return HttpContext.Current.Request.ServerVariables["Remote_Addr"] == "127.0.0.1";
}
/// <summary>
///
/// </summary>
/// <param name="theValue"></param>
/// <returns></returns>
public static string UrlEncode(string theValue)
{
if (theValue == null || theValue.Length == 0)
return "";
if (HttpContext.Current == null)
return theValue;
return HttpContext.Current.Server.UrlEncode(theValue);
}
/// <summary>
///
/// </summary>
/// <param name="hc"></param>
/// <param name="theValue"></param>
/// <returns></returns>
public static string UrlEncode(HttpContext hc, string theValue)
{
if (theValue == null || theValue.Length == 0)
return "";
if (hc == null)
return theValue;
return hc.Server.UrlEncode(theValue);
}
/// <summary>
///
/// </summary>
/// <param name="theValue"></param>
/// <returns></returns>
public static string UrlDecode(string theValue)
{
if (theValue == null || theValue.Length == 0)
return "";
if (HttpContext.Current == null)
return theValue;
return HttpContext.Current.Server.UrlDecode(theValue);
}
/// <summary>
///
/// </summary>
/// <param name="hc"></param>
/// <param name="theValue"></param>
/// <returns></returns>
public static string UrlDecode(HttpContext hc, string theValue)
{
if (theValue == null || theValue.Length == 0)
return "";
if (hc == null)
return theValue;
return hc.Server.UrlDecode(theValue);
}
/// <summary>
/// 获得 当前页面所在根目录地址 ~ /Test/WebCommon/Default.aspx
/// </summary>
/// <returns></returns>
public static string GetPagePath()
{
HttpContext hc = HttpContext.Current;
if (hc == null)
{
throw new Exception
(
"方法:"
+ MethodBase.GetCurrentMethod().ReflectedType.FullName
+ " "
+ MethodBase.GetCurrentMethod().ToString()
+ " 发生异常:HttpContext hc = HttpContext.Current值为null。"
);
}
string strOne = hc.Request.Url.LocalPath;
string strTwo = GetPhyPath(hc);
if (strOne == null || strOne.Length == 0)
return "";
if (strTwo == null || strTwo.Length == 0)
return strOne;
return strOne.Substring((strTwo).Length);
}
/// <summary>
/// 添加参数
/// </summary>
/// <param name="url"></param>
/// <param name="theParam"></param>
/// <returns></returns>
public static string UrlAddParam(string url, string theParam)
{
if (theParam == null || theParam.Length == 0)
return url;
if (url == null || url.Length == 0)
return "?" + theParam;
int idx = url.LastIndexOf('?');
if (idx == -1)
return url + "?" + theParam;
else
return url + "&" + theParam;
}
/// <summary>
/// 转换URL地址
/// </summary>
/// <param name="url"></param>
/// <param name="phyPath"></param>
/// <returns></returns>
public static string ConvertUrl(string url, string phyPath)
{
if (url.StartsWith("~/") || url.StartsWith("-/"))
return phyPath + url.Substring(1);
return url;
}
/// <summary>
/// Ip地址
/// </summary>
/// <returns></returns>
public static string GetIp()
{
if (HttpContext.Current == null || HttpContext.Current.Request == null)
return "未知";
return HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
}
/// <summary>
/// Ip地址
/// </summary>
/// <param name="isRemoteAddr">true:REMOTE_ADDR|false:HTTP_X_FORWARDED_FOR</param>
/// <returns></returns>
public static string GetIp(bool isRemoteAddr)
{
if (HttpContext.Current == null || HttpContext.Current.Request == null)
return "未知";
if (isRemoteAddr)
{
return HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"];
}
else
{
return HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
}
}
/// <summary>
/// 判断当前页面是否接收到了Post请求
/// </summary>
/// <returns>是否接收到了Post请求</returns>
public static bool IsPost()
{
return HttpContext.Current.Request.HttpMethod.Equals("POST");
}
/// <summary>
/// 判断当前页面是否接收到了Get请求
/// </summary>
/// <returns>是否接收到了Get请求</returns>
public static bool IsGet()
{
return HttpContext.Current.Request.HttpMethod.Equals("GET");
}
/// <summary>
/// 通过URL获得HTML内容
/// </summary>
/// <param name="strUrl">URL地址</param>
/// <param name="bKeepAlive"></param>
/// <param name="timeout"></param>
/// <param name="encoding">读取HTML的编码模式</param>
/// <returns></returns>
public static string GetHttpHtmlData
(
string strUrl,
bool bKeepAlive,
int timeout,
Encoding encoding
)
{
if (strUrl == null || strUrl.Length == 0)
throw new Exception("string strUrl 为空。");
if (encoding == null)
encoding = Encoding.Default;
string theResult = null;
HttpWebRequest myReq = null;
HttpWebResponse HttpWResp = null;
Stream myStream = null;
StreamReader sr = null;
try
{
myReq = (HttpWebRequest)HttpWebRequest.Create(strUrl);
myReq.KeepAlive = bKeepAlive;
if (timeout != 0)
{
myReq.Timeout = timeout;
}
HttpWResp
=
(HttpWebResponse)myReq.GetResponse();
myStream = HttpWResp.GetResponseStream();
sr = new StreamReader(myStream, encoding);
StringBuilder strBuilder = new StringBuilder();
while (-1 != sr.Peek())
{
strBuilder.AppendLine(sr.ReadLine());
}
theResult = strBuilder.ToString();
}
catch (Exception ex)
{
throw ex;
}
finally
{
if (sr != null)
{
sr.Close();
sr.Dispose();
sr = null;
}
if (myStream != null)
{
myStream.Close();
myStream.Dispose();
myStream = null;
}
if (HttpWResp != null)
{
HttpWResp.Close();
HttpWResp = null;
}
if (myReq != null)
{
myReq = null;
}
}
return theResult;
}
}
}
标签:
HttpContext的操作 


HttpContext 


asp.net代码 


C#代码 


HttpContext常用方法 


上一条:
下一条:
相关评论
发表评论