HttpRequest 类
關(guān)于此類的介紹:查看HttpRequest類
?點擊查看:HttpRequest中方法的封裝
跟這個類對應的HttpResponse類
定義:使 ASP.NET 能夠讀取客戶端在 Web 請求期間發(fā)送的 HTTP 值。
public sealed class HttpRequest注:本篇主要介紹可以根據(jù)這個類獲取什么信息,只會介紹一些用到的方法。
你先要在引用中添加 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類簡單介紹
定義: 提供統(tǒng)一資源標識符 (URI) 的對象表示形式和對 URI 各部分的輕松訪問。
?屬性: 截取一部分屬性 ? ??
返回字符串類型
?
測試:
頁面1有一個連接到頁面2去
<asp:LinkButton ID="LinkButton1" runat="server" PostBackUrl="~/TestDemo/WebForm2.aspx">LinkButton</asp:LinkButton>頁面2的加載事件把上一個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傳參)
用途:
使用這個很好的可以解決我們登入返回登入頁面的問題。登入返回登入前的頁面還可以使用Session解決,在登入前把頁面信息都保存起來,登入成功后在讀取出來。
注:需要在登入頁面的加載事件把上一個URL用字符串存起來,登入成功了,跳轉(zhuǎn)這個字符串。(寫了登入事件,點擊的時候會讓頁面刷新,上一個頁面就是本事頁面了)
解決方法:
string beforeURL = ""; //第一次進入的時候把之前的頁面存起來protected void Page_Load(object sender, EventArgs e){if (!IsPostBack){if (Request.UrlReferrer != null) //如果這個頁面是第一個打開的,這個值為空{beforeURL = Request.UrlReferrer.ToString();}} } if (beforeURL!=""){Response.Redirect(beforeURL);}else{Response.Redirect("HomePage/Index.aspx");}?
?
?
這三個都是返回字符串類型
備注:原始 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"];第一行代碼會查找鍵"fullname"僅在查詢字符串中;第二行中查找"fullname"中的所有 HTTP 請求集合的鍵。
HttpRequest.Item 屬性 (String)?
從?QueryString、Form、Cookies?或?ServerVariables?集合獲取指定的對象。
?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)建和操作單個 HTTP cookie。
命名空間:???System.Web簡單的設(shè)想Cookies
設(shè)置一個Cookies?
Response.Cookies["one"].Value =Server.UrlEncode("我的Cookie值"); //要存儲中文需要編碼獲取一個Cookies
Response.Write(Server.UrlDecode(Request.Cookies["one"].Value) +"<br>");//進行解碼?
還可以在一個Cookies里面設(shè)置多個健
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 類
提供已上載的客戶端的各個文件的訪問權(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("流對象:"+pf.InputStream + "<br>");Response.Write("字節(jié):"+pf.ContentLength + "<br>");Response.Write("類型:"+pf.ContentType + "<br>");}?
?
基本介紹就到這了。??
?
轉(zhuǎn)載于:https://www.cnblogs.com/Sea1ee/p/7240943.html
創(chuàng)作挑戰(zhàn)賽新人創(chuàng)作獎勵來咯,堅持創(chuàng)作打卡瓜分現(xiàn)金大獎總結(jié)
以上是生活随笔為你收集整理的HttpRequest 类的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 河南大学计算机组成原理,河南大学计算机组
- 下一篇: Android获取手机联系人或通讯录的基