Request是HttpRequest的派生类,主要的工作是从客户端获取信息,包括浏览器的种类,用户输入表单中的数据,Cookies中的数据和客户端认证等。在说Request对象的具体用法之前,先来说一下表单的两种不同的提交方式:Get与Post。
Get将表单中数据的按照variable=value的形式,添加到action所指向的URL后面,并且两者使用“?”连接,而各个变量之间使用“&”连接。Post是将表单中的数据放在form的数据体中,按照变量和值相对应的方式,传递到action所指向URL;Get传送的数据量小,安全性较低,Post方式传送的数据量大,安全性较高。
Get方式提交来的数据在服务器端用Request.QueryString()来获取,例如:
view plaincopy to clipboardprint?
01.<span style="font-size:18px;"> string id=Request.QueryString["name"];
02. string age=Request.QueryString["age"];</span>
<span style="font-size:18px;"> string id=Request.QueryString["name"];
string age=Request.QueryString["age"];</span>
用Post方式提交的数据用Request.Form()来获取,例如:
view plaincopy to clipboardprint?
01.<span style="font-size:18px;"> string aa =Request.Form["name"];
02. string bb=request.Form["age"];</span>
<span style="font-size:18px;"> string aa =Request.Form["name"];
string bb=request.Form["age"];</span>
下面说一下Request对象的调用方法:
1.获得Get方式和Post方式提交数据的方法(如上所述)
2.获得传送的方式:Request.RequestType
3.获取服务器环境变量:
1>获取当前网页虚拟路径:Request.ServerVariables["url"]或者Request.RawUrl
2>获取当前网页实际路径:Request.ServerVariables["path_translated"]或者Request.PhysicalPath
3>获取服务器名:Request.ServerVariables["server_name"]
4>获取服务器IP:Request.UserHostAddress
5>获取客户端的IP地址:Request.getRemoteAddr()
6>获取客户端的名字:Request.getRemoteHost()
4.获取浏览器环境变量:
1>这个浏览器是否支持背景音乐:Request.Browser.BackgroundSounds
2>这个浏览器是否支持框架:Request.Brower.Frames
3>客户用的什么系统:Request.Brower.Platform
5.写入Cookie:Response.Cookies.Add(nc);(nc为一个对象)
6.读出Cookie:
view plaincopy to clipboardprint?
01.<span style="font-size:18px;"> HttpCookie getcook=Request.Cookies["newcookie"];
02. Response.Write(getcook.Values["name"]);......</span>
<span style="font-size:18px;"> HttpCookie getcook=Request.Cookies["newcookie"];
Response.Write(getcook.Values["name"]);......</span>
getProtocol():获取请求使用的通信协议,如http/1.1等
getServletPath():获取请求的JSP也面所在的目录。
getContentLength():获取HTTP请求的长度。
getMethod():获取表单提交信息的方式,如POST或者GET。
getHeader(String s):获取请求中头的值。一般来说,S参数可取的头名有accept,referrer、accept-language、content-type、accept-encoding、user-agent、host、cookie等,比如,S取值user-agent将获得用户的浏览器的版本号等信息。
getHeaderNames():获取头名字的一个枚举。
getHeaders(String s):获取头的全部值的一个枚举。
getRemoteAddr():获取客户的IP地址。
getRemoteHost():获取客户机的名称(如果获取不到,就获取IP地址)。
getServerName():获取服务器的名称。
getServePort():获取服务器的端口。
getPaeameterNames():获取表单提交的信息体部分中name参数值的一个枚举。