HttpRequest 类
关于此类的介绍:查看HttpRequest类
点击查看:HttpRequest中方法的封装
跟这个类对应的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] + "<br>");String[] arr2 = coll.GetValues(arr1[loop1]);for (loop2 = 0; loop2 < arr2.Length; loop2++){Response.Write("Value " + loop2 + ": " + Server.HtmlEncode(arr2[loop2]) + "<br>");}}}
public Uri UrlReferrer { get; }
Url类简单介绍
定义: 提供统一资源标识符 (URI) 的对象表示形式和对 URI 各部分的轻松访问。
属性: 截取一部分属性
返回字符串类型
测试:
页面1有一个连接到页面2去
<asp:LinkButton ID="LinkButton1" runat="server" PostBackUrl="~/TestDemo/WebForm2.aspx">LinkButton</asp:LinkButton>
页面2的加载事件把上一个URL信息输出
protected void Page_Load(object sender, EventArgs e){ Uri MyUrl = Request.UrlReferrer;Response.Write("Referrer URL: " + Server.HtmlEncode(MyUrl.AbsoluteUri) + "<br>");Response.Write("Referrer URL Port: " + Server.HtmlEncode(MyUrl.Port.ToString()) + "<br>");Response.Write("Referrer URL Protocol: " + Server.HtmlEncode(MyUrl.Scheme) + "<br>");Response.Write("Referrer URL: " + Server.HtmlEncode(MyUrl.Query) + "<br>");}
传参是一样的(需要使用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) + "<br>");Response.Write("Referrer2: " + Server.HtmlEncode(Request.PhysicalPath) + "<br>");Response.Write("Referrer URL: " + Server.HtmlEncode(Request.RawUrl) + "<br>");
属性:
Response.Write("Type: " + Server.HtmlEncode(Request.Browser.Type) + "<br>");
Response.Write("Url: " + Server.HtmlEncode(Request.Url.ToString()) + "<br>");
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)
从 QueryString、Form、Cookies 或 ServerVariables 集合获取指定的对象。
Type: System.Collections.Specialized.NameValueCollection
NameValueCollection 类
表示可通过键或索引访问的关联 String 键和 String 值的集合。
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]] + "<br>");}
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] + "<br>");// 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]) + "<br>");}}}
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) +"<br>");//进行解码
还可以在一个Cookies里面设置多个健
HttpCookie myCookie = new HttpCookie("two");myCookie.Values["Name"] = "li";//中文编码myCookie.Values["Age"] = "18";Response.Cookies.Add(myCookie);
Response.Write(Request.Cookies["two"].Value+"<br>");Response.Write(Request.Cookies["two"].Values + "<br>");Response.Write(Request.Cookies["two"].Values["Name"] + "<br>");Response.Write(Request.Cookies["two"]["Age"] + "<br>");
调用封装的方法:
HttpRequestC.WriteCookie("one", "我的Cookied值");HttpRequestC.WriteCookie("two", "li", "Name");HttpRequestC.WriteCookie("two", "187", "Age");
Response.Write(HttpRequestC.GetCookie("one")+"<br>");Response.Write(HttpRequestC.GetCookie("two","Name") + "<br>");Response.Write(HttpRequestC.GetCookie("two", "Age") + "<br>");
Request.Params["xxx"];//通用方法
HttpFileCollection.Item 属性 (String)
HttpPostedFile 类
提供已上载的客户端的各个文件的访问权限。
<asp:FileUpload ID="fileUpload" runat="server" /><asp:FileUpload ID="fileTwo" runat="server" />
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]) + "<br />");Response.Write(" size = " + Files[loop1].ContentLength + "<br />");Response.Write(" content type = " + Files[loop1].ContentType + "<br />");}HttpPostedFile pf = Request.Files["fileTwo"];Response.Write("Name:"+pf.FileName+"<br>");Response.Write("流对象:"+pf.InputStream + "<br>");Response.Write("字节:"+pf.ContentLength + "<br>");Response.Write("类型:"+pf.ContentType + "<br>");}
基本介绍就到这了。
转载于:https://www.cnblogs.com/Sea1ee/p/7240943.html
HttpRequest 类相关推荐
- HttpRequest 与HttpWebRequest 有什么区别
System.Web.HttpRequest是封装浏览器对服务器的请求的,主要用在ASP.NET中,其中包括浏览器请求的网址,查询字符串数据或表单数据等等 而System.Net.HttpWebReq ...
- 一个文件下载的工具类
主类: /** * <p> * Copyright: Copyright (c) 2015 * Company: * Description: 这里写这个文件是干什么用的 * </p ...
- HttpWebRequest类
HttpWebRequest类与HttpRequest类的区别. HttpRequest类的对象用于服务器端,获取客户端传来的请求的信息,包括HTTP报文传送过来的所有信息.而HttpWebReque ...
- quick-cocos2d-x 绑定C++自定义类
初次接触lua和quick-cocos2d-x,想要用用cocosWidget,然后就碰到C++到lua的绑定问题了. 看了下网上的教程,http://cn.cocos2d-x.org/tutoria ...
- DRF-视图类APIView与GenericAPIView
两个基本类视图APIView,GenericAPIView APIView继承了Django中的view 权限指将来如果有注册用户的时候,是否有权限访问到我们的视图 认证是当发过来一个用户名和密码的时 ...
- HttpRequest 和HttpWebRequest的区别(转)
[1]问题: asp.net C# 中HttpRequest 和HttpWebRequest的区别 HttpRequest 与HttpWebRequest 有什么区别? 网上中文的帖子很多,但是答案 ...
- asp.net 中 HttpRequest 中跟URL path 有关的方法和属性 (摘自用怪异的眼光去研究blog)
对于获取URL path,在HttpRequest 类中已经封装好了很多的相关的方法和属性. 可是在使用中,依然容易被搞迷糊,那个方法是获取相对路径的,那个方法是获取绝对的,每次都会晕上一会儿. 今儿 ...
- python drf_067.Python框架Django之DRF视图类
一 关于视图类的一下概念 drf除了在数据序列化部分简写代码以外,还在视图中提供了简写操作.所以在django原有的django.views.View类基础上,drf封装了多个子类出来提供给我们使用. ...
- DAY74-Django框架(五)
一.虚拟环境 对于一些需要跑在不同版本的模块下的项目,可以跑在虚拟环境中运行 创建虚拟环境的两种方法 pychanrm**创建 1.创建项目时选择使用虚拟环境 2.在已创建的项目中设置settin ...
最新文章
- 使用PyTorch时,最常见的4个错误
- DPDK pci驱动探测(十八)
- NDK开发之日志打印
- mapReducer第一个例子WordCount
- 【洛谷P1833】樱花
- 博达路由器如何配置互联网ip_如何成为技术大牛第七步——路由器下接交换机单臂路由配置方法...
- python质量转换程序,Python库的文件转换成MP3和设置它们的质量
- linux每日命令(13):more命令
- 存储函数与存储过程的区别
- 画PCB四层板细节总结
- c语言对文本霍夫曼编码,C语言之霍夫曼编码学习
- matplotlib画三维图
- Swift 模式(Patterns)
- SpringBoot 系列教程(七十七):SpringBoot整合ehcache缓存
- buu-[Zer0pts2020]easy strcmp
- 今日微语早报简报 精选12条新闻摘要 每天一分钟 知晓天下事 3月22日
- opencv 基于ORB特征点图像拼接
- 毛星云Opencv之9.5模板匹配及其实现代码
- 树莓派配置热点官网操作指引
- C语言自定义函数的调用