ASP.NET HttpRequest類介紹
HttpRequest 類
關(guān)于此類的介紹:查看HttpRequest類
點(diǎn)擊查看:HttpRequest中方法的封裝
跟這個(gè)類對(duì)應(yīng)的HttpResponse類
定義:使 ASP.NET 能夠讀取客戶端在 Web 請(qǐng)求期間發(fā)送的 HTTP 值。
public sealed class HttpRequest
注:本篇主要介紹可以根據(jù)這個(gè)類獲取什么信息,只會(huì)介紹一些用到的方法。
你先要在引用中添加 System.Web.然后引用命名空間。
屬性:




public void GetTest()
{
int loop1, loop2;
NameValueCollection coll; //System.Collections.Specialized; 命名空間下
// Load ServerVariable collection into NameValueCollection object.
coll = Request.ServerVariables;
// Get names of all keys into a string array.
String[] arr1 = coll.AllKeys;
for (loop1 = 0; loop1 < arr1.Length; loop1++)
{
Response.Write("Key: " + arr1[loop1] + "<br>");
String[] arr2 = coll.GetValues(arr1[loop1]);
for (loop2 = 0; loop2 < arr2.Length; loop2++)
{
Response.Write("Value " + loop2 + ": " + Server.HtmlEncode(arr2[loop2]) + "<br>");
}
}
}
public Uri UrlReferrer { get; }Url類簡(jiǎn)單介紹
定義: 提供統(tǒng)一資源標(biāo)識(shí)符 (URI) 的對(duì)象表示形式和對(duì) URI 各部分的輕松訪問。


屬性: 截取一部分屬性
返回字符串類型



測(cè)試:
頁面1有一個(gè)連接到頁面2去
<asp:LinkButton ID="LinkButton1" runat="server" PostBackUrl="~/TestDemo/WebForm2.aspx">LinkButton</asp:LinkButton>
頁面2的加載事件把上一個(gè)URL信息輸出
protected void Page_Load(object sender, EventArgs e)
{
Uri MyUrl = Request.UrlReferrer;
Response.Write("Referrer URL: " + Server.HtmlEncode(MyUrl.AbsoluteUri) + "<br>");
Response.Write("Referrer URL Port: " + Server.HtmlEncode(MyUrl.Port.ToString()) + "<br>");
Response.Write("Referrer URL Protocol: " + Server.HtmlEncode(MyUrl.Scheme) + "<br>");
Response.Write("Referrer URL: " + Server.HtmlEncode(MyUrl.Query) + "<br>");
}
傳參是一樣的(需要使用get傳參)
用途:
使用這個(gè)很好的可以解決我們登入返回登入頁面的問題。登入返回登入前的頁面還可以使用Session解決,在登入前把頁面信息都保存起來,登入成功后在讀取出來。
注:需要在登入頁面的加載事件把上一個(gè)URL用字符串存起來,登入成功了,跳轉(zhuǎn)這個(gè)字符串。(寫了登入事件,點(diǎn)擊的時(shí)候會(huì)讓頁面刷新,上一個(gè)頁面就是本事頁面了)
解決方法:
string beforeURL = ""; //第一次進(jìn)入的時(shí)候把之前的頁面存起來
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
if (Request.UrlReferrer != null) //如果這個(gè)頁面是第一個(gè)打開的,這個(gè)值為空
{
beforeURL = Request.UrlReferrer.ToString();
}
}
} if (beforeURL!="")
{
Response.Redirect(beforeURL);
}
else
{
Response.Redirect("HomePage/Index.aspx");
}這三個(gè)都是返回字符串類型


備注:原始 URL 被指以下域信息的 URL 的一部分。在 URL 字符串 http://www.contoso.com/articles/recent.aspx,原始的 URL 是 /articles/recent.aspx。如果存在,原始的 URL 包括查詢字符串。
Response.Write("Referrer1: " + Server.HtmlEncode(Request.PhysicalApplicationPath) + "<br>");
Response.Write("Referrer2: " + Server.HtmlEncode(Request.PhysicalPath) + "<br>");
Response.Write("Referrer URL: " + Server.HtmlEncode(Request.RawUrl) + "<br>");


屬性:

Response.Write("Type: " + Server.HtmlEncode(Request.Browser.Type) + "<br>");

Response.Write("Url: " + Server.HtmlEncode(Request.Url.ToString()) + "<br>");Url: http://localhost:4265/TestDemo/WebForm1.aspx?data=1&ke=good


get傳參
string fullname1 = Request.QueryString["fullname"]; //返回的是string類型 string fullname2 = Request["fullname"];
第一行代碼會(huì)查找鍵"fullname"僅在查詢字符串中;第二行中查找"fullname"中的所有 HTTP 請(qǐng)求集合的鍵。
HttpRequest.Item 屬性 (String)
從QueryString、Form、Cookies或ServerVariables集合獲取指定的對(duì)象。
Type:System.Collections.Specialized.NameValueCollection
NameValueCollection 類
表示可通過鍵或索引訪問的關(guān)聯(lián)String鍵和String值的集合。



post傳參
Form和QueryString是一樣的,都可以使用下面的方法獲取都有的健和值
int loop1 = 0;
NameValueCollection coll = Request.QueryString; //注意引用命名空間
string[] arr1 = coll.AllKeys;
for (loop1 = 0; loop1 < arr1.Length; loop1++)
{
Response.Write("Form: " + arr1[loop1] + ",Vlue:"+ Request.QueryString[arr1[loop1]] + "<br>");
}


protected void Page_Load(object sender, EventArgs e)
{
int loop1, loop2;
NameValueCollection coll;
// Load Header collection into NameValueCollection object.
coll = Request.Headers;
// Put the names of all keys into a string array.
String[] arr1 = coll.AllKeys;
for (loop1 = 0; loop1 < arr1.Length; loop1++)
{
Response.Write("Key: " + arr1[loop1] + "<br>");
// Get all values under this key.
String[] arr2 = coll.GetValues(arr1[loop1]);
for (loop2 = 0; loop2 < arr2.Length; loop2++)
{
Response.Write("Value " + loop2 + ": " + Server.HtmlEncode(arr2[loop2]) + "<br>");
}
}
}


Request.Cookies["XX"];//返回的是HttpCookie類
HttpCookie 類
提供以類型安全的方式來創(chuàng)建和操作單個(gè) HTTP cookie。
命名空間:System.Web


簡(jiǎn)單的設(shè)想Cookies
設(shè)置一個(gè)Cookies
Response.Cookies["one"].Value =Server.UrlEncode("我的Cookie值"); //要存儲(chǔ)中文需要編碼獲取一個(gè)Cookies
Response.Write(Server.UrlDecode(Request.Cookies["one"].Value) +"<br>");//進(jìn)行解碼
還可以在一個(gè)Cookies里面設(shè)置多個(gè)健
HttpCookie myCookie = new HttpCookie("two");
myCookie.Values["Name"] = "li";//中文編碼
myCookie.Values["Age"] = "18";
Response.Cookies.Add(myCookie); Response.Write(Request.Cookies["two"].Value+"<br>");
Response.Write(Request.Cookies["two"].Values + "<br>");
Response.Write(Request.Cookies["two"].Values["Name"] + "<br>");
Response.Write(Request.Cookies["two"]["Age"] + "<br>");
調(diào)用封裝的方法:
HttpRequestC.WriteCookie("one", "我的Cookied值");
HttpRequestC.WriteCookie("two", "li", "Name");
HttpRequestC.WriteCookie("two", "187", "Age"); Response.Write(HttpRequestC.GetCookie("one")+"<br>");
Response.Write(HttpRequestC.GetCookie("two","Name") + "<br>");
Response.Write(HttpRequestC.GetCookie("two", "Age") + "<br>");
Request.Params["xxx"];//通用方法



HttpFileCollection.Item 屬性 (String)

HttpPostedFile 類
提供已上載的客戶端的各個(gè)文件的訪問權(quán)限。

<asp:FileUpload ID="fileUpload" runat="server" />
<asp:FileUpload ID="fileTwo" runat="server" /> protected void LinkButton1_Click(object sender, EventArgs e)
{
int loop1;
HttpFileCollection Files = Request.Files;
string[] arr1 = Files.AllKeys;
for (loop1 = 0; loop1 < arr1.Length; loop1++)
{
Response.Write("File: " + Server.HtmlEncode(arr1[loop1]) + "<br />");
Response.Write(" size = " + Files[loop1].ContentLength + "<br />");
Response.Write(" content type = " + Files[loop1].ContentType + "<br />");
}
HttpPostedFile pf = Request.Files["fileTwo"];
Response.Write("Name:"+pf.FileName+"<br>");
Response.Write("流對(duì)象:"+pf.InputStream + "<br>");
Response.Write("字節(jié):"+pf.ContentLength + "<br>");
Response.Write("類型:"+pf.ContentType + "<br>");
}
基本介紹就到這了。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
WPF框架Prism中ViewModelLocator用法介紹
這篇文章介紹了WPF框架Prism中ViewModelLocator的用法,對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-02-02
ASP.NET中 RadioButtonList 單選按鈕組控件的使用方法
本文主要簡(jiǎn)單介紹RadioButtonList控件的常見屬性和使用方法,希望對(duì)大家有所幫助。2016-04-04
部署ASP.NET?Core程序到Linux系統(tǒng)
這篇文章介紹了部署ASP.NET?Core程序到Linux系統(tǒng)的方法,對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-03-03
Blazor實(shí)現(xiàn)數(shù)據(jù)驗(yàn)證
這篇文章介紹了Blazor實(shí)現(xiàn)數(shù)據(jù)驗(yàn)證的方式,對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-01-01
ASP.NET中CheckBoxList復(fù)選框列表控件詳細(xì)使用方法
本文主要介紹CheckBoxList幾種常見的用法,并做出范例演示供大家參考,希望對(duì)學(xué)習(xí)asp.net的朋友有所幫助。2016-04-04
asp.net UpdaeProgress的簡(jiǎn)單用法
這個(gè)控件相比其他控件,屬性少 使用簡(jiǎn)單,就先把這個(gè)控件的一般使用方法簡(jiǎn)單紀(jì)錄下2008-10-10
.Net行為型設(shè)計(jì)模式之中介者模式(Mediator)
這篇文章介紹了.Net行為型設(shè)計(jì)模式之中介者模式(Mediator),文中通過示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-05-05

