获取HttpServletRequest请求消息头、消息行信息

Phoenix Phoenix 2018.2.5 00:00 浏览(371) 评论 (0)
    //获取请求方式
        System.out.println(req.getMethod());
        //获取项目名称
        System.out.println(req.getContextPath());
        //获取完整请求路径
        System.out.println(req.getRequestURL());
        //获取除了域名外的请求数据
        System.out.println(req.getRequestURI());
        //获取请求参数
        System.out.println(req.getQueryString());
        System.out.println("----------------------------------------------------------");
        //获取请求头
        String header = req.getHeader("user-Agent");
        System.out.println(header);
        header = header.toLowerCase();
        //根据请求头数据判断浏览器类型
        if(header.contains("chrome")){
            System.out.println("您的访问浏览器为谷歌浏览器");
        }else if(header.contains("msie")){
            System.out.println("您的访问浏览器为IE浏览器");
        }else if(header.contains("firefox")){
            System.out.println("您的访问浏览器为火狐浏览器");
        }else{
            System.out.println("您的访问浏览器为其它浏览器");
        }
        System.out.println("----------------------------------------------------------");
        //获取所有的消息头名称
        Enumeration<String> headerNames = req.getHeaderNames();
        //获取获取的消息头名称,获取对应的值,并输出
        while(headerNames.hasMoreElements()){
            String nextElement = headerNames.nextElement();
            System.out.println(nextElement+":"+req.getHeader(nextElement));
        }
        System.out.println("----------------------------------------------------------");
        //根据名称获取此重名的所有数据
        Enumeration<String> headers = req.getHeaders("accept");
        while (headers.hasMoreElements()) {
            String string = (String) headers.nextElement();
            System.out.println(string);
        }

结果如下:

GET
/httpServletRequest
http://localhost:8080/httpServletRequest/index1
/httpServletRequest/index1
name=ckinghan&password=admin
----------------------------------------------------------
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 
(KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36
您的访问浏览器为谷歌浏览器
----------------------------------------------------------
host:localhost:8080
connection:keep-alive
cache-control:max-age=0
upgrade-insecure-requests:1
user-agent:Mozilla/5.0 (Windows NT 10.0; Win64; x64) 
AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36
accept:text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
accept-encoding:gzip, deflate, sdch, br
accept-language:zh-CN,zh;q=0.8
----------------------------------------------------------
text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8

 

本文链接 https://www.mangoxo.com/blog/NJxY3E5p 版权所有,转载请保留地址链接,感谢!

Phoenix 关注
來一遭,做一事。 等一人,愛一次。
  • 959 动态
  • 1,989 相册
  • 139 粉丝
  • 3,051 获赞