細說HTTP-request

      網友投稿 690 2025-04-02

      文章目錄


      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

      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

      細說HTTP-request

      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小時內刪除侵權內容。

      版權聲明:本文內容由網絡用戶投稿,版權歸原作者所有,本站不擁有其著作權,亦不承擔相應法律責任。如果您發現本站中有涉嫌抄襲或描述失實的內容,請聯系我們jiasou666@gmail.com 處理,核實后本網站將在24小時內刪除侵權內容。

      上一篇:關于連接器的選型,這13點必須要考慮
      下一篇:JS閉包
      相關文章
      亚洲熟妇av一区| 精品亚洲麻豆1区2区3区| 亚洲白嫩在线观看| 亚洲va无码va在线va天堂| 国产精品亚洲二区在线| 亚洲熟妇无码AV不卡在线播放| 亚洲精品国产电影午夜| 香蕉视频在线观看亚洲| 久久国产成人亚洲精品影院| 国产精品亚洲片在线花蝴蝶| 亚洲第一成年免费网站| 在线精品亚洲一区二区| 亚洲伊人久久大香线蕉| 亚洲欧洲久久精品| 91亚洲导航深夜福利| 久久久久亚洲AV成人无码 | 亚洲高清毛片一区二区| 亚洲精品免费在线| 亚洲AV日韩AV天堂久久| 亚洲日产无码中文字幕| 亚洲一本到无码av中文字幕| 亚洲四虎永久在线播放| 亚洲中文字幕久久精品无码喷水| 亚洲视频人成在线播放| 亚洲另类少妇17p| 亚洲无码精品浪潮| 亚洲综合无码精品一区二区三区| 国外亚洲成AV人片在线观看| 国产亚洲精品无码拍拍拍色欲| 国产亚洲美女精品久久久| 亚洲色成人WWW永久网站| 亚洲va久久久噜噜噜久久男同| 亚洲AV日韩AV永久无码久久| 亚洲精品一区二区三区四区乱码| 亚洲在成人网在线看| 亚洲色丰满少妇高潮18p| | 亚洲av永久无码| 噜噜噜亚洲色成人网站| 亚洲综合熟女久久久30p| 亚洲欧洲在线观看|