跳至內容

ASP.NET/Request

本頁使用了標題或全文手工轉換
維基教科書,自由的教學讀本

用戶在client使用Web瀏覽器向Web應用程式發出請求時,會將client資訊發給server。server收到一個HTTP請求,包括了全部查詢字串參數或表單參數、Cookie數據以及瀏覽器資訊。在asp.net中執行時把這些請求資訊封裝成Request物件。

Request物件最常用的是Form和QueryString集合。

主要屬性

[編輯]
Request物件的主要屬性
屬性名字 解釋
ApplicationPath 取得伺服器上asp.net應用程式的虛擬應用程式根路徑
Browser 取得有關正在請求的客戶端的瀏覽器功能的資訊
ContentEncoding 取得或設定實體主體的字元集。該屬性值為表示客戶端的字元集Encoding物件
ContentLength 指定客戶端傳送的內容長度,以位元組為單位
ContentType 取得或設定傳入請求的MIME內容類型
Cookies 取得客戶端傳送的Cookie集合
CurrentExecutionFilePath 取得當前請求的虛擬路徑
FilePath 取得當前請求的虛擬路徑
Files 取得客戶端上載的檔案集合
Form 取得表單變數集合
HttpMethod 取得客戶端使用的HTTP數據傳輸方法(如:get、post或head)
Item 取得Cookies、Form、QueryString或ServerVariables集合中指定的物件
Params 取得Cookies、Form、QueryString或ServerVariables項的組合集合
Path 取得當前請求的虛擬路徑
PathInfo 取得具有URL副檔名的資源的附加路徑資訊
PhysicalApplicationPath 取得當前正在執行的伺服器應用程式的根目錄的物理檔案系統路徑
PhysicalPath 取得與請求的URL相對應的物理檔案路徑
QueryString 取得HTTP查詢字串變數集合
RequestType 取得或設定客戶端使用HTTP數據傳輸的方式(get或post)
ServerVariables 取得Web伺服器變數的集合
TotalBytes 取得當前輸入流的位元組數
Url 取得有關當前請求URL的資訊
UserHostAddress 取得遠端客戶端的IP主機地址

主要方法

[編輯]

(1)MapPath(VirtualPath):將當前請求的URL中的虛擬路徑virtualPath對映到伺服器上的物理路徑。參數virtualPath指定當前請求的虛擬路徑,可以是絕對路徑或相對路徑。該方法的返回值為由virtualPath指定的伺服器物理路徑。

(2)SaveAs (Filename,includeHeaders):將http請求儲存; 保存=>zh-mo:儲存到磁碟。參數filename指定物理驅動器路徑,includeHeaders是一個布林值,指定是否應將HTTP檔頭儲存; 保存=>zh-mo:儲存到磁碟。

常用路徑(path)取得方法

[編輯]

假設網址為http://localhost:1897/ News/Press/Content.aspx?id=1019

標題文字; 文本=>zh-mo:文字
跟 Browser Request 的網址相關的屬性與方法 輸出(output)實例 備註
Request.ApplicationPath / 指的是當前的application(應用程式)的目錄
Request.PhysicalPath D:\Projects\Solution\web\News\Press\Content.aspx 磁碟機代號:\父目錄\子目錄\Content.aspx
Request.PhysicalApplicationPath D:\Projects\Solution\web\ 磁碟機代號:\父目錄\子目錄\
Request.CurrentExecutionFilePath /News/Press/Content.aspx
Request.FilePath /News/Press/Content.aspx 對應於iis的虛擬目錄。
Request.Path /News/Press/Content.aspx 當前請求的虛擬路徑。Path 是 FilePath 和 PathInfo 尾部的串聯。*(見下面詳細講解)
Server.MapPath(string url) 例http://www.example.com/1/index.html, 假設你的應用程式在c:/iis/MySite中,那麼就是c:/iis/MySite/1/index.html 將url對映為伺服器上的物理路徑
Request.RawUrl /News/Press/Content.aspx?id=1019
Request.Url.AbsolutePath /News/Press /Content.aspx
Request.Url.AbsoluteUri http://localhost:1897/Content.aspx?id=1019
Request.Url.LocalPath /News/Press//Content.aspx
Request.Url.PathAndQuery /News/Press//Content.aspx?id=1019&uu=77
Request.Url.Scheme http
Request.Url.Host localhost
Request.Url.Port 1987
Request.Url.Authority localhost:1897
Request.Url.Query ?id=1019
Request.Url.Query[id] 1019
Request.Url.Fragments /
News/
Press/
Content.aspx
Request.Url.Segments[0] /
System.IO.Path.GetDirectoryName(Request.PhysicalPath) D:\Projects\Solution\web\News\Press 磁碟機代號:\父目錄\子目錄\
System.IO.Path.GetFileName(Request.PhysicalPath) Content.aspx

如果請求的地址為http://www.cnblogs.com/default.aspx/books 則:

如果請求地址為http://www.cnblogs.com/defaut.aspx?id=1&name=kk則

讀取表單變數四種方式

[編輯]

使用Request.Form屬性讀取表單變數

[編輯]

HtmlForm控制項的Method屬性的預設值為post。在這種情況下,當用戶提交網頁時,表單數據將以HTTP檔頭的形式傳送到伺服器端。此時,可以使用Request物件的Form屬性來讀取表單變數。如:txtUserName和txtPassword的文本框控制項,則可以透過以下形式來讀取它們的值: Request.Form["txtUserName"] ;Request.Form["txtPassword"]

使用Request.QueryString屬性讀取表單變數

[編輯]

如果將HtmlForm控制項的Method屬性設定為get,則當用戶提交網頁時,表單數據將附加在網址後面傳送到伺服器端。在這種情況下,可以使用Request物件的QueryString屬性讀取表單變數。Request.QueryString["txtUserName"] ;Request.QueryString["txtPassword"]

使用Request.Params屬性讀取表單變數

[編輯]

不論HtmlForm控制項的Method屬性取什麼值,都可以使用Request物件的Params屬性來讀取表單變數的內容,如Request.Params["txtPassword"]或者Request.["txtPassword"],優先取得GET方式提交的數據,它會在QueryString、Form、ServerVariable中都按先後順序搜尋一遍。

透過伺服器控制項的屬性直接讀取表單變數

[編輯]

除了以上3種方式之外,也可以透過伺服器控制項的屬性來直接讀取表單變數,這是取得表單數據的最常用、最簡單的方式。例如: txtUserName.Text

Request.ServerVariables集合中取得到的相關資訊

[編輯]

Request.ServerVariables是一個很強大的工具,可以幫助我們取得很多client和web宿主的資訊,有興趣的朋友可以透過以下代碼看看它到底包含什麼資訊

       foreach (string s in Request.ServerVariables)
       {
           Response.Write(s + "  :  " + Request.ServerVariables[s] + "
"); }

伺服器變數名,透過Request.ServerVariables[伺服器變數名]取得的值

  • APPL_MD_PATH : /LM/W3SVC/894523/Root
  • APPL_PHYSICAL_PATH : D:\VssWorkFolder\British_School_MIS\src\WebSite\
  • INSTANCE_META_PATH : /LM/W3SVC/894523
  • LOCAL_ADDR : 192.168.1.6
  • PATH_INFO : /SysOption/BillingSetup1.aspx
  • PATH_TRANSLATED : D:\VssWorkFolder\British_School_MIS\src\WebSite\SysOption\BillingSetup1.aspx
  • REMOTE_ADDR : 192.168.1.6
  • REMOTE_HOST : 192.168.1.6
  • SCRIPT_NAME : /SysOption/BillingSetup1.aspx
  • SERVER_NAME : 192.168.1.6
  • URL : /SysOption/BillingSetup1.aspx

http地址與本機地址的相互轉化

[編輯]

在伺服器端讀取一個檔案,需要把這個檔案的http地址轉化為本機地址。如:

 Response.Write(Request.MapPath(Request.Path)); 

反之,轉換為http地址使用Page.ResolveClientUrl或Page.ResolveUrl:

 Response.Write(Page.ResolveClientUrl("~/a/a.jpg"));      输出为 ../a/a.jpg 
 Response.Write(Page.ResolveUrl("~/a/a.jpg"));              输出为 /a/a.jpg

ServerVariables集合物件中儲存; 保存=>zh-mo:儲存的常用資訊變數
名字 解釋
Request.ServerVariables("Appl_Physical_Path") 與應用程式元數據庫路徑相應的物理路徑
Request.ServerVariables("All_Http") 客戶端傳送的所有HTTP檔頭,字首HTTP_
Request.ServerVariables("All_Raw") 客戶端傳送的所有HTTP檔頭,其結果和客戶端傳送時一樣,沒有字首HTTP_
Request.ServerVariables("Appl_MD_Path") 應用程式的元數據庫路徑
Request.ServerVariables("Auth_Password") 當使用基本驗證模式時,客戶在密碼對話方塊中輸入的密碼
Request.ServerVariables("Auth_Type") 是用戶存取受保護的指令碼時,伺服器用於檢驗用戶的驗證方法
Request.ServerVariables("Auth_User") 代證的用戶名
Request.ServerVariables("Cert_Cookie") 唯一的客戶證書ID號
Request.ServerVariables("Cert_Flag") 客戶證書標誌,如有客戶端證書,則bit0為0如果客戶端證書驗證無效,bit1被設定為1
Request.ServerVariables("Cert_Issuer") 用戶證書中的發行者欄位
Request.ServerVariables("Cert_Keysize") 安全通訊端層連接關鍵字的位數,如128
Request.ServerVariables("Cert_Secretkeysize") 伺服器驗證私人關鍵字的位數如1024
Request.ServerVariables("Cert_Serialnumber") 客戶證書的序列號欄位
Request.ServerVariables("Cert_Server_Issuer") 伺服器證書的發行者欄位
Request.ServerVariables("Cert_Server_Subject") 伺服器證書的主題欄位
Request.ServerVariables("Cert_Subject") 客戶端證書的主題欄位
Request.ServerVariables("Content_Length") 客戶端發出內容的長度
Request.ServerVariables("Content_Type") 客戶傳送的form內容或HTTPPUT的數據類型
Request.ServerVariables("Https") 如果請求穿過安全通道(SSL),則返回ON如果請求來自非安全通道,則返回OFF
Request.ServerVariables("Http_Accept_Encoding") 返回內容如:gzip,deflate
Request.ServerVariables("Http_Accept_Language") 返回內容如:en-us
Request.ServerVariables("Http_Connection") 返回內容:Keep-Alive
Request.ServerVariables("Http_Cookie") 返回包含在request中的cookie字串
Request.ServerVariables("Http_Host") 返回伺服器地址
Request.ServerVariables("Http_User_Agent") 返回內容:Mozilla/4.0(compatible;MSIE6.0;WindowsNT5.1;SV1)
Request.ServerVariables("Https_Keysize") 安全通訊端層連接關鍵字的位數,如128
Request.ServerVariables("Http_Referer") 請求的字串內容
Request.ServerVariables("Https_Secretkeysize") 伺服器驗證私人關鍵字的位數如1024
Request.ServerVariables("Https_Server_Issuer") 伺服器證書的發行者欄位
Request.ServerVariables("Https_Server_Subject") 伺服器證書的主題欄位
Request.ServerVariables("Instance_ID") IIS實例的ID號
Request.ServerVariables("Instance_Meta_Path") 響應請求的IIS實例的元數據庫路徑
Request.ServerVariables("Local_Addr") 返回接受請求的伺服器地址
Request.ServerVariables("Path_Info") 客戶端提供的路徑資訊
Request.ServerVariables("Path_Translated") 透過由虛擬至物理的對映後得到的路徑
Request.ServerVariables("Query_String") 查詢字串內容
Request.ServerVariables("Remote_Addr") 發出請求的遠端主機的IP位址
Request.ServerVariables("Remote_Host") 發出請求的遠端主機名稱
Request.ServerVariables("Request_Method") 提出請求的方法比如GET、HEAD、POST等等
Request.ServerVariables("Server_Name") 伺服器的主機名、DNS地址或IP位址
Request.ServerVariables("Server_Port_Secure") 如果接受請求的伺服器埠為安全埠時,則為1,否則為0
Request.ServerVariables("Server_Protocol") 伺服器使用的協定的名稱和版本
Request.ServerVariables("Server_Software") 應答請求並執行閘道器的伺服器軟件的名稱和版本
Request.ServerVariables("Server_Port") 接受請求的伺服器埠號
Request.ServerVariables("Script_Name") 執行指令碼的名稱
Request.ServerVariables("Url") 返回伺服器地址

取得客戶端瀏覽器資訊

[編輯]

透過Request物件的Browser屬性得到。需要利用Browser屬性生成一個HttpBrowserCapabilities類型的物件實例。

HttpBrowserCapabilities類具有的常用屬性
名字 解釋
Browser 檢測瀏覽器
Version 檢測瀏覽器的版本
Type 檢測瀏覽器的類型
Platform 檢測瀏覽器所在平台
Frames 檢測瀏覽器是否支援Frames
ActiveXControls 檢測瀏覽器是否支援ActiveX外掛程式
Cookies 檢測瀏覽器是否支援Cookies
VBScript 檢測瀏覽器是否支援VBSCRIPT
Javascript 檢測瀏覽器是否支援Javascript

讀取客戶端Cookie

[編輯]

Cookie是在HTTP協定下伺服器或指令碼可以維護客戶工作站上資訊的一種方式。Cookie是由Web伺服器儲存; 保存=>zh-mo:儲存在用戶瀏覽器上的小文字檔案,它可以包含有關用戶的資訊,這些資訊以名/值對的形式儲存在文字檔案中。無論何時,只要用戶連接接到伺服器,Web站點就可以存取Cookie資訊。Cookie儲存; 保存=>zh-mo:儲存在用戶的Cookie檔案中,當下一次用戶返回時,仍然可以對它進行呼叫。Cookies集合是由一些Cookie物件組成的。Cookie物件的類名為HttpCookie。

HttpCookie類的主要屬性
名字 解釋
ExpiresAbsolute 賦一個日期,過了這個日期Cookie就不能再被使用
Comment 取得或設定伺服器可添加到Cookie中的註釋
CommentUri 取得或設定伺服器可透過Cookie來提供的URI註釋
Discard 取得或設定由伺服器設定的丟棄標誌
Domain 定義Cookie要傳送的唯一域
Expired false:過期|沒過期)
Expires 取得或設定Cookie的過期日期和時間(返回DateTime)(預設MinValue,對談Cookie)
HasKeys false)
HttpOnly false)
Item 取得HttpCookie.Values屬性的捷徑(為與老ASP版本相容)
Name 取得或設定Cookie名
Path 取得或設定要與當前Cookie一起傳輸的虛擬路徑。取得或設定Cookie適用於的URI
Port 取得或設定Cookie適用於的TCP埠的列表
Secure 取得或設定是否使用安全通訊端層SSL(即僅透過HTTPS)傳輸Cookie
TimeStamp 取得Cookie作為DateTime發出的時間
Value 取得或設定單個Cookie的值
Values 取得單個Cookie物件所包含的鍵值對的集合
Version 取得或設定Cookie的HTTP狀態維護版本

使用Cookie時,應注意以下幾點

  • 使用Cookie儲存; 保存=>zh-mo:儲存客戶端瀏覽器請求伺服器頁面的請求資訊時,儲存; 保存=>zh-mo:儲存時間的長短取決於Cookie物件的Expires屬性,可以根據需要來設定。若未設定Cookie的失效日期,則它們僅儲存; 保存=>zh-mo:儲存到關閉瀏覽器為止。若將Cookie物件的Expires屬性設定為DateTime.MaxValue,則表示Cookie永遠不會過期。
  • Cookie儲存的數據量有所限制,大多數瀏覽器支援的最大容量為4096位元組,因此不要用Cookie來儲存; 保存=>zh-mo:儲存大量數據。
  • 並非所有瀏覽器都支援Cookie,並且數據是以明文形式儲存; 保存=>zh-mo:儲存在客戶端電腦; 计算机=>zh-mo:電腦中,因此最好不要用Cookie來儲存; 保存=>zh-mo:儲存敏感的未加密數據。
  • 在ASP.NET中有兩個Cookies集合,即:Response物件的Cookies集合和Request物件的Cookies集合,但兩者的作用有所不同,透過前者可以將Cookie寫入客戶端,透過後者可以讀取儲存在客戶端的Cookie。