1. 程式人生 > 程式設計 >ASP.NET HttpRequest類介紹

ASP.NET HttpRequest類介紹

HttpRequest 類

關於此類的介紹:檢視HttpRequest類

點選檢視:HttpRequest中方法的封裝

跟這個類對應的HttpResponse類

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

public sealed class HttpRequest

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

你先要在引用中新增 System.Web.然後引用名稱空間。

屬性:

ASP.NET HttpRequest類介紹

ASP.NET HttpRequest類介紹

ASP.NET HttpRequest類介紹

ASP.NET HttpRequest類介紹

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

ASP.NET HttpRequest類介紹

public Uri UrlReferrer { get; }

Url類簡單介紹

定義: 提供統一資源識別符號 (URI) 的物件表示形式和對 URI 各部分的輕鬆訪問。

ASP.NET HttpRequest類介紹

ASP.NET HttpRequest類介紹

屬性: 擷取一部分屬性

返回字串型別

ASP.NET HttpRequest類介紹

ASP.NET HttpRequest類介紹

ASP.NET HttpRequest類介紹

測試:

頁面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>");
        }

ASP.NET HttpRequest類介紹

傳參是一樣的(需要使用get傳參)

用途:

使用這個很好的可以解決我們登入返回登入頁面的問題。登入返回登入前的頁面還可以使用Session解決,在登入前把頁面資訊都儲存起來,登入成功後在讀取出來。

注:需要在登入頁面的載入事件把上一個URL用字串存起來,登入成功了,跳轉這個字串。(寫了登入事件,點選的時候會讓頁面重新整理,上一個頁面就是本事頁面了)

解決方法:

  string beforeURL = "";  //第一次進入的時候把之前的頁面存起來
        protected void Page_Load(object sender,EventArgs e)
        {
            if (!IsPostBack)
            {
                if (Request.UrlReferrer != null)    //如果這個頁面是第一個開啟的,這個值為空
                {
                    beforeURL = Request.UrlReferrer.ToString();
                }
            }   http://www.cppcns.com
}
 if (beforeURL!="")
                    {
                        Response.Redirect(beforeURL);
                    }
                    else
                    {
                        Response.Redirect("HomePage/Index.aspx");
                    }

這三個都是返回字串型別

ASP.NET HttpRequest類介紹

ASP.NET HttpRequest類介紹

備註:原始 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>");

ASP.NET HttpRequest類介紹

ASP.NET HttpRequest類介紹

ASP.NET HttpRequest類介紹

屬性:

ASP.NET HttpRequest類介紹

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

ASP.NET HttpRequest類介紹

ASP.NET HttpRequest類介紹

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

ASP.NET HttpRequest類介紹

ASP.NET HttpRequest類介紹

get傳參

string fullname1 = Request.QueryString["fullname"];  //返回的是string型別
string fullname2 = Request["fullnamwww.cppcns.come"];

第一行程式碼會查詢鍵"fullname"僅在查詢字串中;第二行中查詢"fullname"中的所有 HTTP 請求集合的鍵。

HttpRequest.Item 屬性 (String)

從QueryString、Form、Cookies或ServerVariables集合獲取指定的物件。

Type:System.Collections.Specialized.NameValueCollection

NameValueCollection 類

表示可通過鍵或索引訪問的關聯String鍵和String值的集合。

ASP.NET HttpRequest類介紹

ASP.NET HttpRequest類介紹

ASP.NET HttpRequest類介紹

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

ASP.NET HttpRequest類介紹

ASP.NET HttpRequest類介紹

ASP.NET HttpRequest類介紹

 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>http://www.cppcns.com;");
                // 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>");
                }
            }
        }

ASP.NET HttpRequest類介紹

ASP.NET HttpRequest類介紹

ASP.NET HttpRequest類介紹

Request.Cookies["XX"];//返回的是HttpCookie類

HttpCookie 類

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

名稱空間:System.Web

ASP.NET HttpRequest類介紹

ASP.NET HttpRequest類介紹

簡單的設想Cookies

設定一個Cookies

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

獲取一個Cookies

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

還可以在一個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>");

ASP.NET HttpRequest類介紹

呼叫封裝的方法:

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

ASP.NET HttpRequest類介紹

Request.Params["xxx"];//通用方法

ASP.NET HttpRequest類介紹

ASP.NET HttpRequest類介紹

ASP.NET HttpRequest類介紹

HttpFileCollection.Item 屬性 (String)

ASP.NET HttpRequest類介紹

HttpPostedFile 類

提供已上載的客戶端的各個檔案的訪問許可權。

ASP.NET HttpRequest類介紹

    <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 />");
        www.cppcns.com        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>");

        }

ASP.NET HttpRequest類介紹

基本介紹就到這了。

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支援我們。