日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程资源 > 编程问答 >内容正文

编程问答

HttpRequest 类

發(fā)布時(shí)間:2023/12/10 编程问答 47 豆豆
生活随笔 收集整理的這篇文章主要介紹了 HttpRequest 类 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

關(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 各部分的輕松訪問(wèn)。

?屬性: 截取一部分屬性 ? ??

返回字符串類型

?

測(cè)試:

頁(yè)面1有一個(gè)連接到頁(yè)面2去

<asp:LinkButton ID="LinkButton1" runat="server" PostBackUrl="~/TestDemo/WebForm2.aspx">LinkButton</asp:LinkButton>

頁(yè)面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è)很好的可以解決我們登入返回登入頁(yè)面的問(wèn)題。登入返回登入前的頁(yè)面還可以使用Session解決,在登入前把頁(yè)面信息都保存起來(lái),登入成功后在讀取出來(lái)。

注:需要在登入頁(yè)面的加載事件把上一個(gè)URL用字符串存起來(lái),登入成功了,跳轉(zhuǎn)這個(gè)字符串。(寫了登入事件,點(diǎn)擊的時(shí)候會(huì)讓頁(yè)面刷新,上一個(gè)頁(yè)面就是本事頁(yè)面了)

解決方法:

string beforeURL = ""; //第一次進(jìn)入的時(shí)候把之前的頁(yè)面存起來(lái)protected void Page_Load(object sender, EventArgs e){if (!IsPostBack){if (Request.UrlReferrer != null) //如果這個(gè)頁(yè)面是第一個(gè)打開(kāi)的,這個(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ò)鍵或索引訪問(wèn)的關(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 類

提供以類型安全的方式來(lái)創(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è)文件的訪問(wèn)權(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>");}

?

?

基本介紹就到這了。??

?

轉(zhuǎn)載于:https://www.cnblogs.com/Sea1ee/p/7240943.html

創(chuàng)作挑戰(zhàn)賽新人創(chuàng)作獎(jiǎng)勵(lì)來(lái)咯,堅(jiān)持創(chuàng)作打卡瓜分現(xiàn)金大獎(jiǎng)

總結(jié)

以上是生活随笔為你收集整理的HttpRequest 类的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。