request 属性提供对 httprequest 类的属性和方法的编程访问。由于 asp教程.net 页包含对 system.web 命名空间(含有 httpcontext 类)的默认引用,因此在 .aspx 页上可以引用 httpcontext 的成员,而不需要对 httpcontext 的完全限定类引用。例如,可只使用 request.browser 获取客户端浏览器的功能。但是,如果要从 asp.net教程 代码隐藏模块中使用 httprequest 的成员,则必须在该模块中包括对 system.web 命名空间的引用,同时还要完全限定对当前活动的请求/响应上下文以及要使用的 system.web 中的类的引用。例如,在代码隐藏页中,必须指定全名 httpcontext.current.request.browser。
using system;
using system.web;
using system.text;namespace pub.class
{
///
/// requests操作类
///
public class request2 {
#region get/getint/getfloat
///
/// 接收传值
///
/// 参数名称
///参数对应的值
static public string get(string varname)
{
string varvalue = "";
if (httpcontext.current.request[varname]!=null)
varvalue = httpcontext.current.request[varname].tostring();
return varvalue;
}
///
/// getint
///
///
///
///
static public int getint(string varname, int defvalue) { return get(varname).toint(defvalue); }
///
/// getfloat
///
///
///
///
static public float getfloat(string varname, int defvalue) { return get(varname).tofloat(defvalue); }
#endregion#region getq/getqint/getqfloat
///
/// 取url上的参数
///
/// 参数名
///返回参数
static public string getq(string varname)
{
string varvalue = "";
if (httpcontext.current.request.querystring[varname] != null)
varvalue = httpcontext.current.request.querystring[varname].tostring();
return varvalue;
}
///
/// getqint
///
///
///
///
static public int getqint(string varname, int defvalue) { return getq(varname).toint(defvalue); }
///
/// getqfloat
///
///
///
///
static public float getqfloat(string varname, int defvalue) { return getq(varname).tofloat(defvalue); }
#endregion#region getf/getfint/getffloat
///
/// 取post提交的数据
///
/// 名称
///返回值
static public string getf(string varname)
{
string varvalue = "";
if (httpcontext.current.request.form[varname]!=null)
varvalue = httpcontext.current.request.form[varname].tostring();
return varvalue;
}
///
/// getfint
///
///
///
///
static public int getfint(string varname, int defvalue) { return getf(varname).toint(defvalue); }
///
/// getffloat
///
///
///
///
static public float getffloat(string varname, int defvalue) { return getf(varname).tofloat(defvalue); }
#endregion#region ispost/isget
///
/// 判断当前页面是否接收到了post请求
///
///是否接收到了post请求
public static bool ispost()
{
return httpcontext.current.request.httpmethod.equals("post");
}
///
/// 判断当前页面是否接收到了get请求
///
///是否接收到了get请求
public static bool isget()
{
return httpcontext.current.request.httpmethod.equals("get");
}
#endregion#region 服务器变量名
///
/// 返回指定的服务器变量信息
///
///
/// 服务器变量名
///服务器变量信息
public static string getserverstring(string strname)
{
if (httpcontext.current.request.servervariables[strname] == null)
return "";
return httpcontext.current.request.servervariables[strname].tostring();
}
#endregion#region getrawurl/isbrowserget/issearchenginesget/getpagename/getqparamcount/getfparamcount/getparamcount/
///
/// 获取当前请求的原始 url(url 中域信息之后的部分,包括查询字符串(如果存在))
///
///原始 url
public static string getrawurl()
{
return httpcontext.current.request.rawurl;
}
///
/// 判断当前访问是否来自浏览器软件
///
///当前访问是否来自浏览器软件
public static bool isbrowserget()
{
string[] browsername = {"ie", "opera", "netscape", "mozilla", "konqueror", "firefox"};
string curbrowser = httpcontext.current.request.browser.type.tolower();
for (int i = 0; i < browsername.length; i++) {
if (curbrowser.indexof(browsername[i]) >= 0) return true;
}
return false;
}
///
/// 判断是否来自搜索引擎链接
///
///是否来自搜索引擎链接
public static bool issearchenginesget() {
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) return true;
}
}
return false;
}
///
/// 获得当前页面的名称
///
///当前页面的名称
public static string getpagename()
{
string [] urlarr = httpcontext.current.request.url.absolutepath.split('/');
return urlarr[urlarr.length - 1].tolower();
}
///
/// 返回表单或url参数的总个数
///
///
public static int getparamcount()
{
return httpcontext.current.request.form.count + httpcontext.current.request.querystring.count;
}
///
/// get paramcount
///
///
public static int getqparamcount() { return (httpcontext.current.request.querystring.count); }
///
/// post paramcount
///
///
public static int getfparamcount() { return (httpcontext.current.request.form.count); }
#endregion#region getcurrentfullhost/gethost/getip/geturl/getreferrer/saverequestfile/getos/getbrowser
///
/// 取全ip包括端口
///
///ip和端口
public static string getcurrentfullhost()
{
httprequest request = httpcontext.current.request;
if (!request.url.isdefaultport)
return string.format("{0}:{1}", request.url.host, request.url.port.tostring());
return request.url.host;
}
///
/// 取主机名
///
///
public static string gethost() { return httpcontext.current.request.url.host; }
///
/// 前台url
///
///
public static string geturl() { return httpcontext.current.request.url.tostring(); }
///
/// 来源url
///
///
public static string getreferrer() {
string str = null;
try {
str = getserverstring("http_referer").trim();
if (str.length==0) str = httpcontext.current.request.urlreferrer.tostring();
} catch { }if (str == null) return "";
return str;
}
///
/// 保存request文件
///
/// 保存到文件名
public static void saverequestfile(string path)
{
if (httpcontext.current.request.files.count > 0) httpcontext.current.request.files[0].saveas(path);
}
#region getip
///
/// 取ip
///
///返回ip
public static string getip() {
string result = string.empty;
result = httpcontext.current.request.servervariables["http_x_forwarded_for"];if (result != null && result != string.empty) {//可能有代理
if (result.indexof(".") == -1) result = null;
else {
if (result.indexof(",") != -1) {//有“,”,估计多个代理。取第一个不是内网的ip。
result = result.replace(" ", "").replace("'", "");
string[] temparyip = result.split(",;".tochararray());
for (int i = 0; i < temparyip.length; i++) {
if (temparyip[i].isip()
&& temparyip[i].substring(0, 3) != "10."
&& temparyip[i].substring(0, 7) != "192.168"
&& temparyip[i].substring(0, 7) != "172.16.")
{
return temparyip[i]; //找到不是内网的地址
}
}
}
else if (result.isip()) //代理即是ip格式
return result;
else
result = null; //代理中的内容 非ip,取ip
}}
string ipaddress = (httpcontext.current.request.servervariables["http_x_forwarded_for"] != null
&& httpcontext.current.request.servervariables["http_x_forwarded_for"] != string.empty)
? httpcontext.current.request.servervariables["http_x_forwarded_for"]
: httpcontext.current.request.servervariables["remote_addr"];if (null == result || result == string.empty)
result = httpcontext.current.request.servervariables["remote_addr"];if (result == null || result == string.empty)
result = httpcontext.current.request.userhostaddress;return result;
}
#endregion
#region getos
///
/// 取操作系统
///
///返回操作系统
public static string getos() {
httpbrowsercapabilities bc = new httpbrowsercapabilities();
bc = system.web.httpcontext.current.request.browser;
return bc.platform;
}
#endregion
#region getbrowser
///
/// 取游览器
///
///返回游览器
public static string getbrowser()
{
httpbrowsercapabilities bc = new httpbrowsercapabilities();
bc = system.web.httpcontext.current.request.browser;
return bc.type;
}
#endregion
#endregion
}
}
asp.net request对象的属性和方法比较多,常用的几个为:useragent 传回客户端浏览器的版本信息,userhostaddress 传回远方客户端机器的主机ip 地址,userhostname 传回远方客户端机器的dns 名称,physicalapplicationpath 传回目前请求网页在server 端的真实路径。
asp.net request对象使用之从浏览器获取数据
利用asp.net request对象方法,可以读取其他页面提交过来的数据。提交的数据有两种形式:一种是通过form表单提交过来,另一种是通过超级链接后面的参数提交过来,两种方式都可以利用request对象读取。