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

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

HttpRequest 类

發布時間:2023/12/10 编程问答 52 豆豆
生活随笔 收集整理的這篇文章主要介紹了 HttpRequest 类 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

關于此類的介紹:查看HttpRequest類

?點擊查看:HttpRequest中方法的封裝

跟這個類對應的HttpResponse類

定義:使 ASP.NET 能夠讀取客戶端在 Web 請求期間發送的 HTTP 值。

public sealed class HttpRequest

注:本篇主要介紹可以根據這個類獲取什么信息,只會介紹一些用到的方法。

你先要在引用中添加 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類簡單介紹

定義: 提供統一資源標識符 (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用字符串存起來,登入成功了,跳轉這個字符串。(寫了登入事件,點擊的時候會讓頁面刷新,上一個頁面就是本事頁面了)

解決方法:

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 類

表示可通過鍵或索引訪問的關聯?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 類

提供以類型安全的方式來創建和操作單個 HTTP cookie。

命名空間:???System.Web

簡單的設想Cookies

設置一個Cookies?

Response.Cookies["one"].Value =Server.UrlEncode("我的Cookie值"); //要存儲中文需要編碼

獲取一個Cookies

Response.Write(Server.UrlDecode(Request.Cookies["one"].Value) +"<br>");//進行解碼

?

還可以在一個Cookies里面設置多個健

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>");

?

?調用封裝的方法:

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 類

提供已上載的客戶端的各個文件的訪問權限。

?

?

<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("字節:"+pf.ContentLength + "<br>");Response.Write("類型:"+pf.ContentType + "<br>");}

?

?

基本介紹就到這了。??

?

轉載于:https://www.cnblogs.com/Sea1ee/p/7240943.html

創作挑戰賽新人創作獎勵來咯,堅持創作打卡瓜分現金大獎

總結

以上是生活随笔為你收集整理的HttpRequest 类的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。