首页 > 其他分享 >Request_获取请求头数据和Request_获取请求体数据

Request_获取请求头数据和Request_获取请求体数据

时间:2022-12-16 20:13:35浏览次数:36  
标签:请求 Request request System 获取 println response

Request_获取请求头数据:

获取请求头数据

方法:

  1.(*)String getHeader(String name):通过请求头的名称获取请求头的值

  2.Enumeration<String> getHeaderNames():获取所有的请求头名称

代码实现:

@WebServlet("/requestDemo1")
public class RequestDemo1 extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //演示获取请求头数据

        //1、获取所有请求头名称
        Enumeration<String> headerNames = request.getHeaderNames();
        //2、遍历
        while (headerNames.hasMoreElements()){
            String name = headerNames.nextElement();
            //根据名称获取请求头
            String value = request.getHeader(name);
            System.out.println(name+"-----"+value);
        }

        //获取请求头数据:user-agent
        String header = request.getHeader("user-agent");
        //判断header的浏览器版本
        if(header.contains("Chrome")){
            //谷歌
            System.out.println("谷歌");
        }else if (header.contains("Firofox")){
            //火狐
            System.out.println("火狐");
        }

        //演示获取请求头数据:referer
        String referer = request.getHeader("referer");
        System.out.println(referer);
        //防盗链
        if (referer!=null){
            if (referer.contains("/")){
                //正常访问
//                System.out.println("播放");
                response.setContentType("text/html;charset=utf-8");
                response.getWriter().write("播放");
            }else {
                //盗链
                System.out.println("去首页");
                response.setContentType("text/html;charset=utf-8");
                response.getWriter().write("播放");
            }
        }

    }
}

login.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
    <form action="/demo3" method="post">
        <input name="username">
        <input type="submit" value="提交">
    </form>

<hr>

    <a href="/requestDemo1">requestdemo1</a>
</body>
</html>

 

 

 

 

 

 

 

Request_获取请求体数据:

获取请求体数据:

 请求体:只有POST请求方式,才有请求体,在请求体中封装了POST请求的请求参数

   步骤:

     1.获取流对象

         BufferedReader  getReader():获取字符输入流,只能操作字符数据

         ServletInputStream getInputSream():获取字符输入流,可以操作所有类型的数据

           1.在文件上传知识后讲解

代码实现:

@WebServlet(name = "RequestDemo5", value = "/RequestDemo5")
public class RequestDemo5 extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //获取请求消息体--请求参数

        //1.获取字符流
        BufferedReader br = request.getReader();
        //2.读取数据
        String line = null;
        while ((line = br.readLine())!= null){
            System.out.println(line);
        }
    }
}

regist.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
    <form action="RequestDemo5" method="post">
        <input type="text" placeholder="请输入用户名" name="username"><br>
        <input type="text" placeholder="请输入密码" name="password"><br>
        <input type="submit" value="注册">
    </form>
</body>
</html>

 

标签:请求,Request,request,System,获取,println,response
From: https://www.cnblogs.com/hungui/p/16988209.html

相关文章