avatar

Request请求消息数据的解析

数据格式

请求消息数据格式为:
1.请求行
2.请求头
3.请求空行
4.请求体
示例:
GET /index.html HTTP/1.1
Host: localhost
User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.106 Safari/537.36
Accept: text/html,image/,/*;q=0.8
Referer: http://localhost/day14/index.html
Accept-Encoding: gzip, deflate, sdch
Accept-Language: zh-CN,zh;q=0.8

获取请求行数据

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//获取请求方式:Get
String method = request.getMethod();
System.out.println(method);
//获取虚拟目录:/day14
String contextPath = request.getContextPath();
System.out.println(contextPath);
//获取Servlet路径:/demo1
String servletPath = request.getServletPath();
System.out.println(servletPath);
//获取get方式请求参数:name = aaa
String queryString = request.getQueryString();
System.out.println(queryString);
//获取请求URI:/day14/demo1
String requestURI = request.getRequestURI();
StringBuffer requestURL = request.getRequestURL();
System.out.println(requestURI);
System.out.println(requestURL);
//获取协议及版本:HTTP/1.1
String protocol = request.getProtocol();
System.out.println(protocol);
//获取客户机的IP地址
String remoteAddr = request.getRemoteAddr();
System.out.println(remoteAddr);
}

获取请求头数据

获取请求所有头数据

1
2
3
4
5
6
7
8
9
10
11
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//获取请求头数据
//获取所有请求头名称
Enumeration<String> headerNames = request.getHeaderNames();
//遍历
while(headerNames.hasMoreElements()){
String name = headerNames.nextElement();
//根据名称获取请求头的值
String value = request.getHeader(name);
System.out.println(name+"---"+value);
}

获取请求头数据 user-agent,实现浏览器的分类

1
2
3
4
5
6
7
8
9
10
11
12
13
14
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//获取请求头数据
String agent = request.getHeader("user-agent");
//判断agent的浏览器版本
if(agent.contains("Chrome")){
//谷歌
System.out.println("谷歌");
}else if(agent.contains("Firofox")){
//火狐
System.out.println("火狐");
}else{
System.out.println("Edge");
}
}

获取请求头数据 referer 实现防盗链

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//获取请求头数据:referer
String referer = request.getHeader("referer");
System.out.println(referer);//http://localhost/day14/login.html

//防盗链
if (referer != null){
if(referer.contains("/day14")){
//正常访问
System.out.println("访问来源正确");
}else{
System.out.println("访问来源错误");
}
}
}

获取请求体

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//获取请求消息体
//获取字符流
BufferedReader br = request.getReader();
//获取字节流
ServletInputStream inputStream = request.getInputStream();
//读取数据
String line = null;
while ((line = br.readLine()) != null){
System.out.println(line);
}
//根据参数名获取参数值
String username = getInitParameter("username");
String password = getInitParameter("password");
System.out.println(username);
System.out.println(password);
}

评论