ASP.NET——B/S模式运行过程(代码阐述)及Get与Post请求

接上文,上文从宏观的角度阐述了B/S运行过程,这一篇从代码的角度来分享一下。

假如淘宝的程序猿做一个登录的功能,写如下代码


登录功能代码


新建一个一般处理程序ShowLogin.ashx,给浏览器回复登录页面Login.html

<%@ WebHandler Language="C#" Class="ShowAdd" %>

using System;
using System.Web;
using System.IO;
public class ShowAdd : IHttpHandler {
    
    //当用户访问此页面时iis自动调用此方法
    public void ProcessRequest (HttpContext context) {
        //浏览器回复文件的类型
        context.Response.ContentType = "text/html";
        //读取模板文件.
        string filePath = context.Request.MapPath("Login.html");
        string fileContent=File.ReadAllText(filePath);
        context.Response.Write(fileContent);
    }

    //获取一个值,该值指示其他请求是否可以使用 IHttpHandler 实例,先不用管
    public bool IsReusable {
        get {
            return false;
        }
    }

}

登录页面Login.html

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <title>登录</title>
</head>
<body>
    <!--表单:收集用户的数据。--->
    <!--此时方法为get--->
    <form method="get"  action="LoginInfo.ashx"><!--action后表示去访问哪一个文件--->
        用户名:<input type="text" name="txtName" /><br />
        密码:<input type="password" name="txtPwd" /><br />
        <input type="submit" value="登录" />
    </form>
</body>
</html>

登录后显示页面LoginInfo.ashx

<%@ WebHandler Language="C#" Class="AddInfo" %>

using System;
using System.Web;

public class AddInfo : IHttpHandler {

    public void ProcessRequest (HttpContext context) {
        //浏览器回复文本类型为字符串
        context.Response.ContentType = "text/plain";
        string userName = context.Request.QueryString["txtName"];//接收的是表单元素name属性的值
        string userPwd = context.Request.QueryString["txtPwd"];
        context.Response.Write("用户名是:"+userName+",密码是:"+userPwd);
    }

    public bool IsReusable {
        get {
            return false;
        }
    }

}

代码执行过程

现在将上述执行过程展开,浏览器与服务器交互的过程如下:


浏览器与服务器交互过程


首先访问ShowLogin.ashx

当用户输入用户名,密码数据后点击登录,根据Login.html中代码

<form method="get"  action="LoginInfo.ashx">

访问LoginInfo.ashx,执行里面的代码流程如下

因为IIS软件本身不识别C#的代码,所以把代码交给了Netframework去执行

可以看出每访问页面一次,浏览器就执行页面中代码一次,通过这两张图,相信大家对B/S工作流程有了更清晰的认识


Get请求与Post请求


不知大家有没有发现在访问http://localhost:2454/RequestMethod/LoginInfo.ashx?txtName=1231&txtPwd=123这个网址的时候,后面会显示用户名及密码,有没有很诧异,没错,这就引出了上述两种请求方式

这不得不提到在Login.html中代码

<form method="get"  action="LoginInfo.ashx">

此时提交的方式为get请求,此时在地址栏会显示用户名及密码,那么当变为Post请求时执行结果会怎么样,咱们改下相应的代码

将Login.html中代码提交方式get变为post

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <title>登录</title>
</head>
<body>
    <!--表单:收集用户的数据。--->
    <!--此时方法为post--->
    <form method="post"  action="LoginInfo.ashx"><!--action后表示去访问哪一个文件--->
        用户名:<input type="text" name="txtName" /><br />
        密码:<input type="password" name="txtPwd" /><br />
        <input type="submit" value="登录" />
    </form>
</body>
</html>

将原有接收get请求的代码注释掉,方法变为context.Request.Form["txtName"]

<%@ WebHandler Language="C#" Class="AddInfo" %>

using System;
using System.Web;

public class AddInfo : IHttpHandler {

    public void ProcessRequest (HttpContext context) {
        context.Response.ContentType = "text/plain";
        //string userName = context.Request.QueryString["txtName"];//接收的是表单元素name属性的值
        //string userPwd = context.Request.QueryString["txtPwd"];

        string userName = context.Request.Form["txtName"];
        string userPwd = context.Request.Form["txtPwd"];
        context.Response.Write("用户名是:"+userName+",密码是:"+userPwd);
    }

    public bool IsReusable {
        get {
            return false;
        }
    }

}

现在执行一遍,效果如下

此时的地址栏中便没有了用户名及密码,网址为http://localhost:2454/RequestMethod/LoginInfo.ashx,而用户名及密码被放在了请求报文的请求体中

根据上述改代码及显示情况,对Get与Post请求作一个总结

发布了156 篇原创文章 · 获赞 49 · 访问量 5万+

猜你喜欢

转载自blog.csdn.net/shang_0122/article/details/103098638