request请求对象实例

时间:2021-08-02 22:31:08

request请求对象实例

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="DemoRequest.aspx.cs" Inherits="WebApplication1.DemoRequest" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div> <asp:Button ID="Button1" runat="server" onclick="Button1_Click"
Text="request上的属性" />
<br />
用户名:<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<br />
<asp:Button ID="Button2" runat="server" onclick="Button2_Click" Text="登录" /> </div>
</form>
</body>
</html>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls; namespace WebApplication1
{
public partial class DemoRequest : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{ } protected void Button1_Click(object sender, EventArgs e)
{
//request为请求对象。请求者给我带来的一系列信息 Response.Write("请求的路径:" + Request.Path);
Response.Write("<br/>请求者的ip:" + Request.UserHostAddress);
Response.Write("<br/>请求者的主机名:" + Request.UserHostName);
Response.Write("<br/>请求者的浏览器:" + Request.Browser.Browser);
string p = Request.Path;//请求的路径 这里得到的是虚拟路径(站点路径 )
string s = Request.MapPath(p);//能将虚拟路径 转换为物理路径(磁盘路径)
Response.Write("<br/>物理路径:" + s); } protected void Button2_Click(object sender, EventArgs e)
{
//获取提交的数据
string un= Request["TextBox1"];//get 方式与post提交的都可以
Response.Write("<br/>request得到的值:" + un);
un= Request.QueryString["TextBox1"]; //get方式提交过来的值
Response.Write("<br/>QueryString得到的值:" + un);
un = Request.Form["TextBox1"]; //post方式提交过来的
Response.Write("<br/>Form得到的值:" + un); }
}
}