博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
HttpRequest 类
阅读量:4309 次
发布时间:2019-06-06

本文共 6533 字,大约阅读时间需要 21 分钟。

关于此类的介绍:

 点击查看:

跟这个类对应的HttpResponse类

定义:使 ASP.NET 能够读取客户端在 Web 请求期间发送的 HTTP 值。

public sealed class HttpRequest

注:本篇主要介绍可以根据这个类获取什么信息,只会介绍一些用到的方法。

你先要在引用中添加 System.Web.然后引用命名空间。

属性:

 

 

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] + "
"); String[] arr2 = coll.GetValues(arr1[loop1]); for (loop2 = 0; loop2 < arr2.Length; loop2++) { Response.Write("Value " + loop2 + ": " + Server.HtmlEncode(arr2[loop2]) + "
"); } } }

 

 

public Uri UrlReferrer { get; }

 Url类简单介绍

定义: 提供统一资源标识符 (URI) 的对象表示形式和对 URI 各部分的轻松访问。

 属性: 截取一部分属性     

返回字符串类型

 

测试:

页面1有一个连接到页面2去

LinkButton

页面2的加载事件把上一个URL信息输出

protected void Page_Load(object sender, EventArgs e)        {                     Uri MyUrl = Request.UrlReferrer;            Response.Write("Referrer URL: " + Server.HtmlEncode(MyUrl.AbsoluteUri) + "
"); Response.Write("Referrer URL Port: " + Server.HtmlEncode(MyUrl.Port.ToString()) + "
"); Response.Write("Referrer URL Protocol: " + Server.HtmlEncode(MyUrl.Scheme) + "
"); Response.Write("Referrer URL: " + Server.HtmlEncode(MyUrl.Query) + "
"); }

传参是一样的(需要使用get传参)

用途:

使用这个很好的可以解决我们登入返回登入页面的问题。登入返回登入前的页面还可以使用Session解决,在登入前把页面信息都保存起来,登入成功后在读取出来。

注:需要在登入页面的加载事件把上一个URL用字符串存起来,登入成功了,跳转这个字符串。(写了登入事件,点击的时候会让页面刷新,上一个页面就是本事页面了)

解决方法:

string beforeURL = "";  //第一次进入的时候把之前的页面存起来        protected void Page_Load(object sender, EventArgs e)        {            if (!IsPostBack)            {                if (Request.UrlReferrer != null)    //如果这个页面是第一个打开的,这个值为空                {                    beforeURL = Request.UrlReferrer.ToString();                }            }             }
if (beforeURL!="")                    {                        Response.Redirect(beforeURL);                    }                    else                    {                        Response.Redirect("HomePage/Index.aspx");                    }

 

 

 

这三个都是返回字符串类型

备注:原始 URL 被指以下域信息的 URL 的一部分。 在 URL 字符串 http://www.contoso.com/articles/recent.aspx,原始的 URL 是 /articles/recent.aspx。 如果存在,原始的 URL 包括查询字符串。

 

Response.Write("Referrer1: " + Server.HtmlEncode(Request.PhysicalApplicationPath) + "
"); Response.Write("Referrer2: " + Server.HtmlEncode(Request.PhysicalPath) + "
"); Response.Write("Referrer URL: " + Server.HtmlEncode(Request.RawUrl) + "
");

 

 

 

 

属性:

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

 

 

 

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

 

 

 

 

get传参

string fullname1 = Request.QueryString["fullname"];  //返回的是string类型string fullname2 = Request["fullname"];

第一行代码会查找键"fullname"仅在查询字符串中;第二行中查找"fullname"中的所有 HTTP 请求集合的键。

HttpRequest.Item 属性 (String) 

从 、、 或  集合获取指定的对象。

 Type: 

NameValueCollection 类

表示可通过键或索引访问的关联  键和  值的集合。

 

 

 

 

 

 

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

 

 

 

 

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

 

 

 

 

 

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

 

HttpCookie 类

提供以类型安全的方式来创建和操作单个 HTTP cookie。

命名空间:   

简单的设想Cookies

设置一个Cookies 

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

获取一个Cookies

Response.Write(Server.UrlDecode(Request.Cookies["one"].Value) +"
");//进行解码

 

还可以在一个Cookies里面设置多个健

HttpCookie myCookie = new HttpCookie("two");            myCookie.Values["Name"] = "li";//中文编码            myCookie.Values["Age"] = "18";            Response.Cookies.Add(myCookie);
Response.Write(Request.Cookies["two"].Value+"
"); Response.Write(Request.Cookies["two"].Values + "
"); Response.Write(Request.Cookies["two"].Values["Name"] + "
"); Response.Write(Request.Cookies["two"]["Age"] + "
");

 

 调用封装的方法:

HttpRequestC.WriteCookie("one", "我的Cookied值");            HttpRequestC.WriteCookie("two", "li", "Name");            HttpRequestC.WriteCookie("two", "187", "Age");

 

Response.Write(HttpRequestC.GetCookie("one")+"
"); Response.Write(HttpRequestC.GetCookie("two","Name") + "
"); Response.Write(HttpRequestC.GetCookie("two", "Age") + "
");

 

 

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

 

 

 

HttpFileCollection.Item 属性 (String)

 

HttpPostedFile 类

提供已上载的客户端的各个文件的访问权限。

 

 

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]) + "
"); Response.Write(" size = " + Files[loop1].ContentLength + "
"); Response.Write(" content type = " + Files[loop1].ContentType + "
"); } HttpPostedFile pf = Request.Files["fileTwo"]; Response.Write("Name:"+pf.FileName+"
"); Response.Write("流对象:"+pf.InputStream + "
"); Response.Write("字节:"+pf.ContentLength + "
"); Response.Write("类型:"+pf.ContentType + "
"); }

 

 

基本介绍就到这了。  

 

转载于:https://www.cnblogs.com/Sea1ee/p/7240943.html

你可能感兴趣的文章
4.java设计模式-原型模式(prototype)
查看>>
Javaee -----01----javaee的环境搭建和html标签 ...
查看>>
JVM内存分布和垃圾回收
查看>>
DOM操作指令
查看>>
PHPCMS快速建站系列之类别调用及类别显示页面
查看>>
《第二章 感知机》
查看>>
HomeWork1_Login in
查看>>
javascript中的类
查看>>
新词发现博文收集
查看>>
input text focus去掉默认光影
查看>>
使用JsonP进行跨域请求
查看>>
HDU 5317 RGCDQ (数论素筛)
查看>>
学习JSP(一)
查看>>
node安装-Win+Linux+Mac osx
查看>>
cookie和session笔记
查看>>
Java中使用注释
查看>>
构建你的第一个App
查看>>
Network Mapper 嗅探工具
查看>>
linux下定时执行任务的方法
查看>>
ASP.NET MVC 常用内置验证特性 简介
查看>>