对象化的Http和请求对象HttpRequest

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了对象化的Http和请求对象HttpRequest相关的知识,希望对你有一定的参考价值。

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

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

    // Summary:
    //     Enables ASP.NET to read the HTTP values sent by a client during a Web request.
    public sealed class HttpRequest

对于每一个请求参数,HttpRuntime将创建一个类型为HttpRequest的对象。

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

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

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

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace HttpRequestDemo
{
    public partial class Test : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            StringBuilder sb = new StringBuilder();
            //请求的类型,get或者post,可读写
            sb.AppendFormat("RequestType:{0}<br/>", Request.RequestType);
            //Httpq请求的方法,get,post或者head,只读
            sb.AppendFormat("HttpMethod:{0}<br/>", Request.HttpMethod);
            //通过工作者对象HttpWorkerRequest获取的请求原始字符串形式的Url地址
            sb.AppendFormat("RawUrl:{0}<br/>", Request.RawUrl);
            //System.Uri类型的请求地址
            sb.AppendFormat("Url:{0}<br/>", Request.Url);
            //取得导致当前请求的上一个地址
            sb.AppendFormat(":{0}<br/>", Request.UrlReferrer);
            //请求的虚拟路径,不包含请求参数信息
            sb.AppendFormat("FilePath:{0}<br/>", Request.FilePath);
            //请求的虚拟路径,通常情况下,相当于FilePath,当通过Server.Execute执行的时候,FilePath不变,而CurrentExecutionFilePath表示原来的虚拟路径。
            sb.AppendFormat("CurrentExecutionFilePath:{0}<br/>", Request.CurrentExecutionFilePath);
            //请求的虚拟路径,包含请求参数信息
            sb.AppendFormat("Path:{0}<br/>", Request.Path);
            //yi~开头的相对虚拟路径
            sb.AppendFormat("AppRelativeCurrentExecutionFilePath:{0}<br/>", Request.AppRelativeCurrentExecutionFilePath);
            //web应用程序所在的文件系统的目录
            sb.AppendFormat("PhysicalApplicationPath:{0}<br/>", Request.PhysicalApplicationPath);
            //当前请求虚拟路径所对应的文件系统路径
            sb.AppendFormat("PhysicalPath:{0}<br/>", Request.PhysicalPath);
            //取得url请求参数 NameValueCollection类型
            foreach (string item in Request.QueryString.Keys)
            {
                sb.AppendFormat("QueryString:Name={0},value={1}<br/>", item, Request.QueryString[item]);
            }
            //客户端可以接受的类型
            sb.AppendFormat("AcceptTypes:{0}<br/>", string.Join(",", Request.AcceptTypes));
            //通过form表单提交的参数 NameValueCollection类型
            foreach (string item in Request.Form.Keys)
            {
                sb.AppendFormat("Form:Name={0},value={1}<br/>", item, Request.Form[item]);
            }
            //请求头中的参数
            foreach (string item in Request.Headers.Keys)
            {
                sb.AppendFormat("Headers:Name={0},value={1}<br/>", item, Request.Headers[item]);
            }
            //请求中的cookies
            foreach (string item in Request.Cookies.Keys)
            {
                sb.AppendFormat("Cookies:Name={0},value={1}<br/>", item, Request.Cookies[item]);
            }
            //web服务器提供的参数
            //foreach (string item in Request.ServerVariables.Keys)
            //{
            //    sb.AppendFormat("ServerVariables:Name={0},value={1}<br/>", item, Request.ServerVariables[item]);
            //}
            //客户端上传的文件集合
            foreach (HttpPostedFile item in Request.Files)
            {
                sb.AppendFormat("FileName:{0}<br/>", item.FileName);
            }
            //提供统一的方法来访问 QueryString,Form,Cookies,ServerVariables中的参数
            foreach (string item in Request.Params.Keys)
            {
                sb.AppendFormat("Params:Name={0},value={1}<br/>", item, Request.Params[item]);
            }
            //主体部分
            //请求主体的编码
            sb.AppendFormat("ContentEncoding:{0}<br/>", Request.ContentEncoding);
            //请求主体的长度
            sb.AppendFormat("ContentLength:{0}<br/>", Request.ContentLength);
            // 请求主体的类型
            sb.AppendFormat("ContentType:{0}<br/>", Request.ContentType);
            //浏览器类型
            sb.AppendFormat("Browser:{0}<br/>", Request.Browser);
            //用户代理,即浏览器
            sb.AppendFormat("UserAgent:{0}<br/>", Request.UserAgent);
            //客户端地址
            sb.AppendFormat("UserHostAddress:{0}<br/>", Request.UserHostAddress);
            //客户端DNS
            sb.AppendFormat(":{0}<br/>", Request.UserHostName);
            //客户端语言
            sb.AppendFormat("UserLanguages:{0}<br/>", string.Join(",", Request.UserLanguages));

            Response.Write(sb.ToString());
        }
    }
}

输出结果

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

以上是关于对象化的Http和请求对象HttpRequest的主要内容,如果未能解决你的问题,请参考以下文章

django HttpRequest对象

Python入门自学进阶-Web框架——4HttpRequest和HttpResponse及模板

Django HttpRequest对象

django 获取request请求对象及response响应对象中的各种属性值

django框架-request对象

django-request对象