request 屬性提供對 httprequest 類(lèi)的屬性和方法的編程訪(fǎng)問(wèn)。由于 asp教程.net 頁(yè)包含對 system.web 命名空間(含有 httpcontext 類(lèi))的默認引用,因此在 .aspx 頁(yè)上可以引用 httpcontext 的成員,而不需要對 httpcontext 的完全限定類(lèi)引用。例如,可只使用 request.browser 獲取客戶(hù)端瀏覽器的功能。但是,如果要從 asp.net教程 代碼隱藏模塊中使用 httprequest 的成員,則必須在該模塊中包括對 system.web 命名空間的引用,同時(shí)還要完全限定對當前活動(dòng)的請求/響應上下文以及要使用的 system.web 中的類(lèi)的引用。例如,在代碼隱藏頁(yè)中,必須指定全名 httpcontext.current.request.browser。
using system;
using system.web;
using system.text;namespace pub.class
{
/// <summary>
/// requests操作類(lèi)
/// </summary>
public class request2 {
#region get/getint/getfloat
/// <summary>
/// 接收傳值
/// </summary>
/// <param name="varname">參數名稱(chēng)</param>
/// <returns>參數對應的值</returns>
static public string get(string varname)
{
string varvalue = "";
if (httpcontext.current.request[varname]!=null)
varvalue = httpcontext.current.request[varname].tostring();
return varvalue;
}
/// <summary>
/// getint
/// </summary>
/// <param name="varname"></param>
/// <param name="defvalue"></param>
/// <returns></returns>
static public int getint(string varname, int defvalue) { return get(varname).toint(defvalue); }
/// <summary>
/// getfloat
/// </summary>
/// <param name="varname"></param>
/// <param name="defvalue"></param>
/// <returns></returns>
static public float getfloat(string varname, int defvalue) { return get(varname).tofloat(defvalue); }
#endregion#region getq/getqint/getqfloat
/// <summary>
/// 取url上的參數
/// </summary>
/// <param name="varname">參數名</param>
/// <returns>返回參數</returns>
static public string getq(string varname)
{
string varvalue = "";
if (httpcontext.current.request.querystring[varname] != null)
varvalue = httpcontext.current.request.querystring[varname].tostring();
return varvalue;
}
/// <summary>
/// getqint
/// </summary>
/// <param name="varname"></param>
/// <param name="defvalue"></param>
/// <returns></returns>
static public int getqint(string varname, int defvalue) { return getq(varname).toint(defvalue); }
/// <summary>
/// getqfloat
/// </summary>
/// <param name="varname"></param>
/// <param name="defvalue"></param>
/// <returns></returns>
static public float getqfloat(string varname, int defvalue) { return getq(varname).tofloat(defvalue); }
#endregion#region getf/getfint/getffloat
/// <summary>
/// 取post提交的數據
/// </summary>
/// <param name="varname">名稱(chēng)</param>
/// <returns>返回值</returns>
static public string getf(string varname)
{
string varvalue = "";
if (httpcontext.current.request.form[varname]!=null)
varvalue = httpcontext.current.request.form[varname].tostring();
return varvalue;
}
/// <summary>
/// getfint
/// </summary>
/// <param name="varname"></param>
/// <param name="defvalue"></param>
/// <returns></returns>
static public int getfint(string varname, int defvalue) { return getf(varname).toint(defvalue); }
/// <summary>
/// getffloat
/// </summary>
/// <param name="varname"></param>
/// <param name="defvalue"></param>
/// <returns></returns>
static public float getffloat(string varname, int defvalue) { return getf(varname).tofloat(defvalue); }
#endregion#region ispost/isget
/// <summary>
/// 判斷當前頁(yè)面是否接收到了post請求
/// </summary>
/// <returns>是否接收到了post請求</returns>
public static bool ispost()
{
return httpcontext.current.request.httpmethod.equals("post");
}
/// <summary>
/// 判斷當前頁(yè)面是否接收到了get請求
/// </summary>
/// <returns>是否接收到了get請求</returns>
public static bool isget()
{
return httpcontext.current.request.httpmethod.equals("get");
}
#endregion#region 服務(wù)器變量名
/// <summary>
/// 返回指定的服務(wù)器變量信息
///
/// </summary>
/// <param name="strname">服務(wù)器變量名</param>
/// <returns>服務(wù)器變量信息</returns>
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/
/// <summary>
/// 獲取當前請求的原始 url(url 中域信息之后的部分,包括查詢(xún)字符串(如果存在))
/// </summary>
/// <returns>原始 url</returns>
public static string getrawurl()
{
return httpcontext.current.request.rawurl;
}
/// <summary>
/// 判斷當前訪(fǎng)問(wèn)是否來(lái)自瀏覽器軟件
/// </summary>
/// <returns>當前訪(fǎng)問(wèn)是否來(lái)自瀏覽器軟件</returns>
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;
}
/// <summary>
/// 判斷是否來(lái)自搜索引擎鏈接
/// </summary>
/// <returns>是否來(lái)自搜索引擎鏈接</returns>
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;
}
/// <summary>
/// 獲得當前頁(yè)面的名稱(chēng)
/// </summary>
/// <returns>當前頁(yè)面的名稱(chēng)</returns>
public static string getpagename()
{
string [] urlarr = httpcontext.current.request.url.absolutepath.split('/');
return urlarr[urlarr.length - 1].tolower();
}
/// <summary>
/// 返回表單或url參數的總個(gè)數
/// </summary>
/// <returns></returns>
public static int getparamcount()
{
return httpcontext.current.request.form.count + httpcontext.current.request.querystring.count;
}
/// <summary>
/// get paramcount
/// </summary>
/// <returns></returns>
public static int getqparamcount() { return (httpcontext.current.request.querystring.count); }
/// <summary>
/// post paramcount
/// </summary>
/// <returns></returns>
public static int getfparamcount() { return (httpcontext.current.request.form.count); }
#endregion#region getcurrentfullhost/gethost/getip/geturl/getreferrer/saverequestfile/getos/getbrowser
/// <summary>
/// 取全ip包括端口
/// </summary>
/// <returns>ip和端口</returns>
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;
}
/// <summary>
/// 取主機名
/// </summary>
/// <returns></returns>
public static string gethost() { return httpcontext.current.request.url.host; }
/// <summary>
/// 前臺url
/// </summary>
/// <returns></returns>
public static string geturl() { return httpcontext.current.request.url.tostring(); }
/// <summary>
/// 來(lái)源url
/// </summary>
/// <returns></returns>
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;
}
/// <summary>
/// 保存request文件
/// </summary>
/// <param name="path">保存到文件名</param>
public static void saverequestfile(string path)
{
if (httpcontext.current.request.files.count > 0) httpcontext.current.request.files[0].saveas(path);
}
#region getip
/// <summary>
/// 取ip
/// </summary>
/// <returns>返回ip</returns>
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) {//有“,”,估計多個(gè)代理。取第一個(gè)不是內網(wǎng)的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]; //找到不是內網(wǎng)的地址
}
}
}
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
/// <summary>
/// 取操作系統
/// </summary>
/// <returns>返回操作系統</returns>
public static string getos() {
httpbrowsercapabilities bc = new httpbrowsercapabilities();
bc = system.web.httpcontext.current.request.browser;
return bc.platform;
}
#endregion
#region getbrowser
/// <summary>
/// 取游覽器
/// </summary>
/// <returns>返回游覽器</returns>
public static string getbrowser()
{
httpbrowsercapabilities bc = new httpbrowsercapabilities();
bc = system.web.httpcontext.current.request.browser;
return bc.type;
}
#endregion
#endregion
}
}
asp.net request對象的屬性和方法比較多,常用的幾個(gè)為:useragent 傳回客戶(hù)端瀏覽器的版本信息,userhostaddress 傳回遠方客戶(hù)端機器的主機ip 地址,userhostname 傳回遠方客戶(hù)端機器的dns 名稱(chēng),physicalapplicationpath 傳回目前請求網(wǎng)頁(yè)在server 端的真實(shí)路徑。
asp.net request對象使用之從瀏覽器獲取數據
利用asp.net request對象方法,可以讀取其他頁(yè)面提交過(guò)來(lái)的數據。提交的數據有兩種形式:一種是通過(guò)form表單提交過(guò)來(lái),另一種是通過(guò)超級鏈接后面的參數提交過(guò)來(lái),兩種方式都可以利用request對象讀取。
聯(lián)系客服