HttpRequest对象

在面向对象的语言中,有种“万物皆对象”的说法。在上篇文章中介绍了HttpRuntime类,在该类收到请求之后,立即通过HttpWorkerRequest工作者对象对传递的参数进行分析和分解,创建方便网站应用程序处理的对象。与此相关的对象主要有,请求和响应两个对象。即HttpRequest和HttpResponse类型。

HttpRequest定义在命名空间System.Web下

这个类定义了大量的属性,用于表示关于Http请求的参数。这些参数来源于工作者对象HttpWorkerRequest,但HttpRequest提供了更加简便的方式。

Http的请求消息包括三部分,分别是:请求行,请求头,请求体。

下面看一下HttpRequest的常见属性。

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 using System.Web;
 6 using System.Web.UI;
 7 using System.Web.UI.WebControls;
 8 
 9 namespace HttpRequestDemo
10 {
11     public partial class Test : System.Web.UI.Page
12     {
13         protected void Page_Load(object sender, EventArgs e)
14         {
15             StringBuilder sb = new StringBuilder();
16             //请求的类型,get或者post,可读写
17             sb.AppendFormat("RequestType:{0}
", Request.RequestType); 18 //Httpq请求的方法,get,post或者head,只读 19 sb.AppendFormat("HttpMethod:{0}
", Request.HttpMethod); 20 //通过工作者对象HttpWorkerRequest获取的请求原始字符串形式的Url地址 21 sb.AppendFormat("RawUrl:{0}
", Request.RawUrl); 22 //System.Uri类型的请求地址 23 sb.AppendFormat("Url:{0}
", Request.Url); 24 //取得导致当前请求的上一个地址 25 sb.AppendFormat(":{0}
", Request.UrlReferrer); 26 //请求的虚拟路径,不包含请求参数信息 27 sb.AppendFormat("FilePath:{0}
", Request.FilePath); 28 //请求的虚拟路径,通常情况下,相当于FilePath,当通过Server.Execute执行的时候,FilePath不变,而CurrentExecutionFilePath表示原来的虚拟路径。 29 sb.AppendFormat("CurrentExecutionFilePath:{0}
", Request.CurrentExecutionFilePath); 30 //请求的虚拟路径,包含请求参数信息 31 sb.AppendFormat("Path:{0}
", Request.Path); 32 //yi~开头的相对虚拟路径 33 sb.AppendFormat("AppRelativeCurrentExecutionFilePath:{0}
", Request.AppRelativeCurrentExecutionFilePath); 34 //web应用程序所在的文件系统的目录 35 sb.AppendFormat("PhysicalApplicationPath:{0}
", Request.PhysicalApplicationPath); 36 //当前请求虚拟路径所对应的文件系统路径 37 sb.AppendFormat("PhysicalPath:{0}
", Request.PhysicalPath); 38 //取得url请求参数 NameValueCollection类型 39 foreach (string item in Request.QueryString.Keys) 40 { 41 sb.AppendFormat("QueryString:Name={0},value={1}
", item, Request.QueryString[item]); 42 } 43 //客户端可以接受的类型 44 sb.AppendFormat("AcceptTypes:{0}
", string.Join(",", Request.AcceptTypes)); 45 //通过form表单提交的参数 NameValueCollection类型 46 foreach (string item in Request.Form.Keys) 47 { 48 sb.AppendFormat("Form:Name={0},value={1}
", item, Request.Form[item]); 49 } 50 //请求头中的参数 51 foreach (string item in Request.Headers.Keys) 52 { 53 sb.AppendFormat("Headers:Name={0},value={1}
", item, Request.Headers[item]); 54 } 55 //请求中的cookies 56 foreach (string item in Request.Cookies.Keys) 57 { 58 sb.AppendFormat("Cookies:Name={0},value={1}
", item, Request.Cookies[item]); 59 } 60 //web服务器提供的参数 61 //foreach (string item in Request.ServerVariables.Keys) 62 //{ 63 // sb.AppendFormat("ServerVariables:Name={0},value={1}
", item, Request.ServerVariables[item]);
64 //} 65 //客户端上传的文件集合 66 foreach (HttpPostedFile item in Request.Files) 67 { 68 sb.AppendFormat("FileName:{0}
", item.FileName); 69 } 70 //提供统一的方法来访问 QueryString,Form,Cookies,ServerVariables中的参数 71 foreach (string item in Request.Params.Keys) 72 { 73 sb.AppendFormat("Params:Name={0},value={1}
", item, Request.Params[item]); 74 } 75 //主体部分 76 //请求主体的编码 77 sb.AppendFormat("ContentEncoding:{0}
", Request.ContentEncoding); 78 //请求主体的长度 79 sb.AppendFormat("ContentLength:{0}
", Request.ContentLength); 80 // 请求主体的类型 81 sb.AppendFormat("ContentType:{0}
", Request.ContentType); 82 //浏览器类型 83 sb.AppendFormat("Browser:{0}
", Request.Browser); 84 //用户代理,即浏览器 85 sb.AppendFormat("UserAgent:{0}
", Request.UserAgent); 86 //客户端地址 87 sb.AppendFormat("UserHostAddress:{0}
", Request.UserHostAddress); 88 //客户端DNS 89 sb.AppendFormat(":{0}
", Request.UserHostName); 90 //客户端语言 91 sb.AppendFormat("UserLanguages:{0}
", string.Join(",", Request.UserLanguages)); 92 93 Response.Write(sb.ToString()); 94 } 95 } 96 }

输出结果

RequestType:GET
HttpMethod:GET
RawUrl:/Test.aspx
Url:http://192.168.1.105:8888/Test.aspx
:
FilePath:/Test.aspx
CurrentExecutionFilePath:/Test.aspx
Path:/Test.aspx
AppRelativeCurrentExecutionFilePath:~/Test.aspx
PhysicalApplicationPath:D:\DeepLearn\
PhysicalPath:D:\DeepLearn\Test.aspx
AcceptTypes:text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
Headers:Name=Cache-Control,value=max-age=0
Headers:Name=Connection,value=keep-alive
Headers:Name=Accept,value=text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
Headers:Name=Accept-Encoding,value=gzip, deflate, sdch
Headers:Name=Accept-Language,value=zh-CN,zh;q=0.8
Headers:Name=Host,value=192.168.1.105:8888
Headers:Name=User-Agent,value=Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/47.0.2526.106 Safari/537.36
Headers:Name=Upgrade-Insecure-Requests,value=1
Params:Name=ALL_HTTP,value=HTTP_CACHE_CONTROL:max-age=0 HTTP_CONNECTION:keep-alive HTTP_ACCEPT:text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8 HTTP_ACCEPT_ENCODING:gzip, deflate, sdch HTTP_ACCEPT_LANGUAGE:zh-CN,zh;q=0.8 HTTP_HOST:192.168.1.105:8888 HTTP_USER_AGENT:Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/47.0.2526.106 Safari/537.36 HTTP_UPGRADE_INSECURE_REQUESTS:1 
Params:Name=ALL_RAW,value=Cache-Control: max-age=0 Connection: keep-alive Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8 Accept-Encoding: gzip, deflate, sdch Accept-Language: zh-CN,zh;q=0.8 Host: 192.168.1.105:8888 User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/47.0.2526.106 Safari/537.36 Upgrade-Insecure-Requests: 1 
Params:Name=APPL_MD_PATH,value=/LM/W3SVC/4/ROOT
Params:Name=APPL_PHYSICAL_PATH,value=D:\DeepLearn\
Params:Name=AUTH_TYPE,value=
Params:Name=AUTH_USER,value=
Params:Name=AUTH_PASSWORD,value=
Params:Name=LOGON_USER,value=
Params:Name=REMOTE_USER,value=
Params:Name=CERT_COOKIE,value=
Params:Name=CERT_FLAGS,value=
Params:Name=CERT_ISSUER,value=
Params:Name=CERT_KEYSIZE,value=
Params:Name=CERT_SECRETKEYSIZE,value=
Params:Name=CERT_SERIALNUMBER,value=
Params:Name=CERT_SERVER_ISSUER,value=
Params:Name=CERT_SERVER_SUBJECT,value=
Params:Name=CERT_SUBJECT,value=
Params:Name=CONTENT_LENGTH,value=0
Params:Name=CONTENT_TYPE,value=
Params:Name=GATEWAY_INTERFACE,value=CGI/1.1
Params:Name=HTTPS,value=off
Params:Name=HTTPS_KEYSIZE,value=
Params:Name=HTTPS_SECRETKEYSIZE,value=
Params:Name=HTTPS_SERVER_ISSUER,value=
Params:Name=HTTPS_SERVER_SUBJECT,value=
Params:Name=INSTANCE_ID,value=4
Params:Name=INSTANCE_META_PATH,value=/LM/W3SVC/4
Params:Name=LOCAL_ADDR,value=192.168.1.105
Params:Name=PATH_INFO,value=/Test.aspx
Params:Name=PATH_TRANSLATED,value=D:\DeepLearn\Test.aspx
Params:Name=QUERY_STRING,value=
Params:Name=REMOTE_ADDR,value=192.168.1.105
Params:Name=REMOTE_HOST,value=192.168.1.105
Params:Name=REMOTE_PORT,value=3445
Params:Name=REQUEST_METHOD,value=GET
Params:Name=SCRIPT_NAME,value=/Test.aspx
Params:Name=SERVER_NAME,value=192.168.1.105
Params:Name=SERVER_PORT,value=8888
Params:Name=SERVER_PORT_SECURE,value=0
Params:Name=SERVER_PROTOCOL,value=HTTP/1.1
Params:Name=SERVER_SOFTWARE,value=Microsoft-IIS/10.0
Params:Name=URL,value=/Test.aspx
Params:Name=HTTP_CACHE_CONTROL,value=max-age=0
Params:Name=HTTP_CONNECTION,value=keep-alive
Params:Name=HTTP_ACCEPT,value=text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
Params:Name=HTTP_ACCEPT_ENCODING,value=gzip, deflate, sdch
Params:Name=HTTP_ACCEPT_LANGUAGE,value=zh-CN,zh;q=0.8
Params:Name=HTTP_HOST,value=192.168.1.105:8888
Params:Name=HTTP_USER_AGENT,value=Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/47.0.2526.106 Safari/537.36
Params:Name=HTTP_UPGRADE_INSECURE_REQUESTS,value=1
ContentEncoding:System.Text.UTF8Encoding
ContentLength:0
ContentType:
Browser:System.Web.Mobile.MobileCapabilities
UserAgent:Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/47.0.2526.106 Safari/537.36
UserHostAddress:192.168.1.105
UserHostName:192.168.1.105
UserLanguages:zh-CN,zh;q=0.8

 

转:https://www.cnblogs.com/wolf-sun/p/5204778.html

你可能感兴趣的:(HttpRequest对象)