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请求保存到磁盘。参数filename指定物理驱动器路径,includeHeaders是一个布尔值,指定是否应将HTTP标头保存到磁盘。

常用路径(path)获取方法[编辑]

假设网址为http://localhost:1897/ News/Press/Content.aspx?id=1019

标题文本
跟 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集合对象中保存的常用信息变量
名字 解释
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服务器保存在用户浏览器上的小文本文件,它可以包含有关用户的信息,这些信息以名/值对的形式储存在文本文件中。无论何时,只要用户连接接到服务器,Web站点就可以访问Cookie信息。Cookie保存在用户的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保存客户端浏览器请求服务器页面的请求信息时,保存时间的长短取决于Cookie对象的Expires属性,可以根据需要来设置。若未设置Cookie的失效日期,则它们仅保存到关闭浏览器为止。若将Cookie对象的Expires属性设置为DateTime.MaxValue,则表示Cookie永远不会过期。
  • Cookie存储的数据量有所限制,大多数浏览器支持的最大容量为4096字节,因此不要用Cookie来保存大量数据。
  • 并非所有浏览器都支持Cookie,并且数据是以明文形式保存在客户端计算机中,因此最好不要用Cookie来保存敏感的未加密数据。
  • 在ASP.NET中有两个Cookies集合,即:Response对象的Cookies集合和Request对象的Cookies集合,但两者的作用有所不同,通过前者可以将Cookie写入客户端,通过后者可以读取存储在客户端的Cookie。