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 = ; loop1 < arr1.Length; loop1++)
{
Response.Write("Key: " + arr1[loop1] + "<br>");
String[] arr2 = coll.GetValues(arr1[loop1]);
for (loop2 = ; 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 = ;
NameValueCollection coll = Request.QueryString; //注意引用命名空间
string[] arr1 = coll.AllKeys;
for (loop1 = ; 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 = ; loop1 < arr1.Length; loop1++)
{
Response.Write("Key: " + arr1[loop1] + "<br>");
// Get all values under this key.
String[] arr2 = coll.GetValues(arr1[loop1]);
for (loop2 = ; 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"] = "";
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", "", "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 = ; 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>"); }
基本介绍就到这了。
HttpRequest 类的更多相关文章
- ASP.NET -- WebForm -- HttpRequest类的方法和属性
ASP.NET -- WebForm -- HttpRequest类的方法和属性 1. HttpRequest类的方法(1) BinaryRead: 执行对当前输入流进行指定字节数的二进制读取. ( ...
- C# 之 HttpRequest 类
Request对象派生自HttpRequest类,使 ASP.NET 能够读取客户端在 Web 请求期间发送的 HTTP 值,从客户端获取信息,浏览器的种类,用户输入表单的数据,Cooki ...
- HttpWebRequest类与HttpRequest类的区别
HttpRequest类的对象用于服务器端,获取客户端传来的请求的信息,包括HTTP报文传送过来的所有信息.而HttpWebRequest用于客户端,拼接请求的HTTP报文并发送等. HttpWebR ...
- C#,WebRequest类、HttpWebRequest类与HttpRequest类的区别
C#,WebRequest类和HttpWebRequest类的区别? httpWebRequest是webRequest的子类,httpWebRequest是基于http协议的 . HttpWebRe ...
- (4)ASP.NET HttpRequest 类
HttpRequest 类的主要作用是读取客户端在 Web 请求期间发送的 HTTP 值. https://msdn.microsoft.com/zh-cn/library/system.web.ht ...
- .net学习笔记----HttpRequest类
一.HttpRequest的作用 HttpRequest的作用是令到Asp.net能够读取客户端发送HTTP值.比如表单.URL.Cookie传递过来的参数. 返回字符串的那些值就不说了,那些基本上都 ...
- HttpRequest类
一.HttpRequest的作用 HttpRequest的作用是令到Asp.net能够读取客户端发送HTTP值.比如表单.URL.Cookie传递过来的参数. 返回字符串的那些值就不说了,那些基本上都 ...
- .net学习笔记----HttpRequest,WebRequest,HttpWebRequest区别
WebRequest是一个虚类/基类,HttpWebRequest是WebRequest的具体实现 HttpRequest类的对象用于服务器端,获取客户端传来的请求的信息,包括HTTP报文传送过来的所 ...
- java http工具类和HttpUrlConnection上传文件分析
利用java中的HttpUrlConnection上传文件,我们其实只要知道Http协议上传文件的标准格式.那么就可以用任何一门语言来模拟浏览器上传文件.下面有几篇文章从http协议入手介绍了java ...
随机推荐
- 可能是最详细的 Hexo + GitHub Pages 搭建博客的教程
前言:博主目前大三,Web 前端爱好者.写博客的好处,不是为了写而写,而是一个记录思想的过程.不要考虑它能带给你什么,而是你自己从中收获了什么. 最近刚好有空,于是就参照网上的各种教程,搭建了一个博客 ...
- ASP.NET MVC5(五):身份验证、授权
使用Authorize特性进行身份验证 通常情况下,应用程序都是要求用户登录系统之后才能访问某些特定的部分.在ASP.NET MVC中,可以通过使用Authorize特性来实现,甚至可以对整个应用程序 ...
- TCP连接中time_wait在开发中的影响-搜人以鱼不如授之以渔
根据TCP协议定义的3次握手断开连接规定,发起socket主动关闭的一方socket将进入TIME_WAIT状态,TIME_WAIT状态将持续2个MSL(Max Segment Lifetime),T ...
- 自动安装lnmp
注:需先上传各安装包至服务器.#!/bin/bash #! auto install lnmp #! 安装依赖环境 yum -y groupinstall "X Software Devel ...
- 谈谈Nancy中让人又爱又恨的Diagnostics【上篇】
前言 在Nancy中有个十分不错的功能-Diagnostics,可以说这个功能让人又爱又恨. 或许我们都做过下面这样的一些尝试: 记录某一个功能用到的相关技术信息 记录下网站的访问记录 全局配置某些框 ...
- JSONArray用法jquery循环list<Map>对象
controoler中 List<Map<String,Object>> resList =(List<Map<String,Object>>)resM ...
- 响应式、手机端、自适应 百分比实现div等宽等高的方法
在百分比布局中, 有时候会遇见一个头疼的问题,就是如果某个布局是正方形的话,我们在这种情况下考虑到适应各种媒体尺寸,又不能给它定固定的宽高. 之前遇见过纯色布局的结果我就用纯色图片代替实现的,现在有了 ...
- 带你重拾JavaScript(2)之console的你所不知道的功能
JavaScript最常用的调试工具就是console.info()了.console是浏览器中window对象的属性之一,由浏览器对象模型(BOM)提供,作用是访问浏览器控制台,你可以通过conso ...
- JavaScript开发者必备的10个sublime的插件
http://www.codeceo.com/article/10-js-sublime-text-plugins.html
- 记一次SAP新业务开发项目
直到笔者写这篇博文的时候,这个开发项目名义上已经上线,但其实开发以及优化的工作还在继续,数据的修复也仍在继续... IT系统环境很简单,一个基于JAVA+Mysql的Web平台,一个是宇宙第一的SAP ...