细说HTTP-request

网友投稿 537 2022-05-30

文章目录

HTTP概念:

特点:

历史版本:

请求消息数据格式

1. 请求行

2. 请求头:客户端浏览器告诉服务器一些信息

3. 请求空行

4. 请求体(正文):

Request

HTTP概念:

Hyper Text Transfer Protocol 超文本传输协议

传输协议:定义了,客户端和服务器端通信时,发送数据的格式

特点:

1. 基于TCP/IP的高级协议 2. 默认端口号:80 3. 基于请求/响应模型的:一次请求对应一次响应 4. 无状态的:每次请求之间相互独立,不能交互数据

1

2

3

4

历史版本:

1.0:每一次请求响应都会建立新的连接 1.1:复用连接

1

2

请求消息数据格式

请求方式 请求url 请求协议/版本

GET /login.html HTTP/1.1

请求方式:

HTTP协议有7中请求方式,常用的有2种

GET:

1. 请求参数在请求行中,在url后。

2. 请求的url长度有限制的

3. 不太安全

POST:

1. 请求参数在请求体中

2. 请求的url长度没有限制的

3. 相对安全

请求头名称: 请求头值

常见的请求头:

User-Agent:浏览器告诉服务器,我访问你使用的浏览器版本信息

* 可以在服务器端获取该头的信息,解决浏览器的兼容性问题

Referer:http://localhost/login.html

告诉服务器,我(当前请求)从哪里来?

作用:

1. 防盗链:

2. 统计工作:

空行,就是用于分割POST请求的请求头,和请求体的。

封装POST请求消息的请求参数的

> username=zhangsan

1

字符串格式:

POST /login.html HTTP/1.1 Host: localhost User-Agent: Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:60.0) Gecko/20100101 Firefox/60.0 Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8 Accept-Language: zh-CN,zh;q=0.8,zh-TW;q=0.7,zh-HK;q=0.5,en-US;q=0.3,en;q=0.2 Accept-Encoding: gzip, deflate Referer: http://localhost/login.html Connection: keep-alive Upgrade-Insecure-Requests: 1

1

2

3

4

5

6

7

8

9

10

11

Request

request对象和response对象的原理

1. request和response对象是由服务器创建的。我们来使用它们

2. request对象是来获取请求消息,response对象是来设置响应消息

request对象继承体系结构

ServletRequest -- 接口 | 继承 HttpServletRequest -- 接口 | 实现 org.apache.catalina.connector.RequestFacade 类(tomcat)

1

2

3

4

5

request功能:

1. 获取请求消息数据

1. 获取请求行数据

* GET /day14/demo1?name=zhangsan HTTP/1.1

方法:

1. 获取请求方式 :GET * String getMethod() 2. (*)获取虚拟目录:/day14 * String getContextPath() 3. 获取Servlet路径: /demo1 * String getServletPath() 4. 获取get方式请求参数:name=zhangsan * String getQueryString() 5. (*)获取请求URI:/day14/demo1 * String getRequestURI(): /day14/demo1 * StringBuffer getRequestURL() :http://localhost/day14/demo1 * URL:统一资源定位符 : http://localhost/day14/demo1 中华人民共和国 * URI:统一资源标识符 : /day14/demo1 共和国 6. 获取协议及版本:HTTP/1.1 * String getProtocol() 7. 获取客户机的IP地址: * String getRemoteAddr()

1

2

3

4

5

6

7

8

9

10

11

12

13

14

细说HTTP-request

15

16

17

18

19

20

21

22

protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { String method= req.getMethod(); String contextPath = req.getContextPath(); String servletPath = req.getServletPath(); String queryString = req.getQueryString(); StringBuffer requestURL = req.getRequestURL(); String requestURI = req.getRequestURI(); String protocol =req.getProtocol(); String remoteAddr= req.getRemoteAddr(); System.out.println(method); System.out.println(contextPath); System.out.println(servletPath); System.out.println(queryString); System.out.println(requestURL); System.out.println(requestURI); System.out.println(protocol); System.out.println(remoteAddr); } GET /JavaWeb_tomcat_war_exploded /request1 name=zhangsan http://localhost:8080/JavaWeb_tomcat_war_exploded/request1 /JavaWeb_tomcat_war_exploded/request1 HTTP/1.1 0:0:0:0:0:0:0:1

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

26

27

28

29

获取请求头数据

* 方法:

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

Enumeration getHeaderNames():获取所有的请求头名称

protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { //1,获取请求头数据 //Enumeration 就可以理解为一个叠加器 Enumeration headnames =req.getHeaderNames(); //2,遍历 while(headnames.hasMoreElements()){ String name= headnames.nextElement(); String value = req.getHeader(name); System.out.println(name+"->"+value); } } host->localhost:8080 connection->keep-alive cache-control->max-age=0 sec-ch-ua->"Chromium";v="86", "\"Not\\A;Brand";v="99", "Google Chrome";v="86" sec-ch-ua-mobile->?0 upgrade-insecure-requests->1 user-agent->Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.183 Safari/537.36 accept->text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9 sec-fetch-site->none sec-fetch-mode->navigate sec-fetch-user->?1 sec-fetch-dest->document accept-encoding->gzip, deflate, br accept-language->zh-CN,zh;q=0.9,en-US;q=0.8,en;q=0.7 cookie->JSESSIONID=E34512754ECBA609DF9B26E1630E6A72

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

26

27

28

29

30

31

32

protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { String agent = req.getHeader("user-agent"); if(agent.contains("Chrome")){ System.out.println("Chrome-----"); }else if(agent.contains("Firefox")){ System.out.println("Firefox"); } } protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { String referer= req.getHeader("referer"); if(agent.contains("/JavaWeb_tomcat_war_exploded/request1")){ System.out.println("ok-----"); }else { System.out.println("No-------"); } }

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

获取请求体数据:

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

步骤:

获取流对象

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

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

再从流对象中拿数据

Title


@WebServlet(urlPatterns = "/request4") public class RequestDemo4 extends HttpServlet { @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { BufferedReader br= req.getReader(); String line =null; //复完值后判断 while((line= br.readLine())!=null){ System.out.println(line); } }

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

26

27

28

29

30

31

其他功能:

获取请求参数通用方式:不论get还是post请求方式都可以使用下列方法来获取请求参数

String getParameter(String name):根据参数名称获取参数值username=zs&password=123

String[] getParameterValues(String name):根据参数名称获取参数值的数组 hobby=xx&hobby=game

Enumeration getParameterNames():获取所有请求的参数名称

Map getParameterMap():获取所有参数的map集合


学习 游戏
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { System.out.println("------------"); String[] hobbies = req.getParameterValues("hobby"); for (String hobby:hobbies){ System.out.println(hobby); } Enumeration parameterNames= req.getParameterNames(); while(parameterNames.hasMoreElements()){ String name= parameterNames.nextElement(); System.out.println(name); String value = req.getParameter(name); System.out.println(value); } Map parameterMap =req.getParameterMap(); //便利 Set keyset = parameterMap.keySet(); for (String name : keyset){ String[] values = parameterMap.get(name); System.out.println(name); for(String value: values){ System.out.println(value); } System.out.println("----------------"); } }

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

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

中文乱码问题:

get方式:tomcat 8 已经将get方式乱码问题解决了

post方式:会乱码

解决:在获取参数前,设置request的编码request.setCharacterEncoding("utf-8");

2. 请求转发:一种在服务器内部的资源跳转方式

步骤:

通过request对象获取请求转发器对象:RequestDispatcher getRequestDispatcher(String path)

使用RequestDispatcher对象来进行转发:forward(ServletRequest request, ServletResponse response)

req. getRequestDispatcher(String path).forward(ServletRequest request, ServletResponse response);

1

特点:

浏览器地址栏路径不发生变化

只能转发到当前服务器内部资源中。

转发是同一次请求

共享数据:

域对象:一个有作用范围的对象,可以在范围内共享数据

request域:代表一次请求的范围,一般用于请求转发的多个资源中共享数据

方法:

void setAttribute(String name,Object obj):存储数据

Object getAttitude(String name):通过键获取值

void removeAttribute(String name):通过键移除键值对

获取ServletContext

ServletContext getServletContext()

ServletContext sc=req.getServletContext()

1

HTTP TCP/IP

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:我用过的设计模式(7)--享元模式
下一篇:spring-boot-plus后台快速开发框架1.0.0.RELEASE发布了
相关文章