首页 > 代码库 > day9http协议

day9http协议

 Http协议入门

2.1 什么是http协议

                                     http协议: 对浏览器客户端 和  服务器端 之间数据传输的格式规范

 技术分享

 

 2.2 查看http协议的工具

                                     1)使用火狐的firebug插件(右键->firebug->网络)

                                     2)使用谷歌的“审查元素”

                                     3)使用系统自带的telnet工具(远程访问工具)                              

                                                        a)telnet localhost 8080      访问tomcat服务器

                                                        b)ctrl+]     回车          可以看到回显

                                                        c)输入请求内容

GET /day09/hello HTTP/1.1Host: localhost:8080

                    d)回车,即可查看到服务器响应信息。

2.3 http协议内容

请求(浏览器-》服务器)GET /day09/hello HTTP/1.1Host: localhost:8080User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:35.0) Gecko/20100101 Firefox/35.0Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8Accept-Language: zh-cn,en-us;q=0.8,zh;q=0.5,en;q=0.3Accept-Encoding: gzip, deflateConnection: keep-alive
响应(服务器-》浏览器)HTTP/1.1 200 OKServer: Apache-Coyote/1.1Content-Length: 24Date: Fri, 30 Jan 2015 01:54:57 GMTthis is hello servlet!!!

3 Http请求

GET /day09/hello HTTP/1.1               -请求行Host: localhost:8080                    --请求头(多个key-value对象)User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:35.0) Gecko/20100101 Firefox/35.0Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8Accept-Language: zh-cn,en-us;q=0.8,zh;q=0.5,en;q=0.3Accept-Encoding: gzip, deflateConnection: keep-alive                                    --一个空行name=eric&password=123456             --(可选)实体内容

3.1 请求行

                            GET /day09/hello HTTP/1.1    

              #http协议版本

                   http1.0:当前浏览器客户端与服务器端建立连接之后,只能发送一次请求,一次请求之后连接关闭。

                   http1.1:当前浏览器客户端与服务器端建立连接之后,可以在一次连接中发送多次请求。(基本都使用1.1)

 

              #请求资源

                                     URL:  统一资源定位符。http://localhost:8080/day09/testImg.html。只能定位互联网资源。是URI                                                         的子集。

                                     URI: 统一资源标记符。/day09/hello。用于标记任何资源。可以是本地文件系统,局域网的资源(//192.168.14.10/myweb/index.html),                                                   可以是互联网。

              #请求方式

                            常见的请求方式: GET 、 POST、 HEAD、 TRACE、 PUT、 CONNECT 、DELETE 

 

                            常用的请求方式: GET  和 POST     

 

                            表单提交:

                                     <form action="提交地址" method="GET/POST">

 

                                     <form>

 

                            GET   vs  POST 区别

 

                            1)GET方式提交

                                               a)地址栏(URI)会跟上参数数据。以?开头,多个参数之间以&分割。

GET /day09/testMethod.html?name=eric&password=123456 HTTP/1.1Host: localhost:8080User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:35.0) Gecko/20100101 Firefox/35.0Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8Accept-Language: zh-cn,en-us;q=0.8,zh;q=0.5,en;q=0.3Accept-Encoding: gzip, deflateReferer: http://localhost:8080/day09/testMethod.htmlConnection: keep-alive

                       b)GET提交参数数据有限制,不超过1KB。

                                               c)GET方式不适合提交敏感密码。

                                               d)注意: 浏览器直接访问的请求,默认提交方式是GET方式

2)POST方式提交

                                     a)参数不会跟着URI后面。参数而是跟在请求的实体内容中。没有?开头,多个参数之间以&分割。

POST /day09/testMethod.html HTTP/1.1Host: localhost:8080User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:35.0) Gecko/20100101 Firefox/35.0Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8Accept-Language: zh-cn,en-us;q=0.8,zh;q=0.5,en;q=0.3Accept-Encoding: gzip, deflateReferer: http://localhost:8080/day09/testMethod.htmlConnection: keep-alivename=eric&password=123456

                b)POST提交的参数数据没有限制。

                                               c)POST方式提交敏感数据。

3.2 请求头

技术分享
 1 Accept: text/html,image/*      -- 浏览器接受的数据类型 2 Accept-Charset: ISO-8859-1     -- 浏览器接受的编码格式 3 Accept-Encoding: gzip,compress  --浏览器接受的数据压缩格式 4 Accept-Language: en-us,zh-       --浏览器接受的语言 5 Host: www.it315.org:80          --(必须的)当前请求访问的目标地址(主机:端口) 6 If-Modified-Since: Tue, 11 Jul 2000 18:23:51 GMT  --浏览器最后的缓存时间 7 Referer: http://www.it315.org/index.jsp      -- 当前请求来自于哪里 8 User-Agent: Mozilla/4.0 (compatible; MSIE 5.5; Windows NT 5.0)  --浏览器类型 9 Cookie:name=eric                     -- 浏览器保存的cookie信息10 Connection: close/Keep-Alive            -- 浏览器跟服务器连接状态。close: 连接关闭  keep-alive:保存连接。11 Date: Tue, 11 Jul 2000 18:23:51 GMT      -- 请求发出的时间
View Code

3.3 实体内容

                                     只有POST提交的参数会放到实体内容中

 

 3.4 HttpServletRequest对象

                            HttpServletRequest对象作用是用于获取请求数据。

 

                                        核心的API:

                                               请求行:

                                                        request.getMethod();   请求方式

                                                        request.getRequetURI()   / request.getRequetURL()   请求资源

                                                        request.getProtocol()   请求http协议版本

                                              

                                               请求头:

                                                        request.getHeader("名称")   根据请求头获取请求值

                                                        request.getHeaderNames()    获取所有的请求头名称

 

                                               实体内容:

                                                        request.getInputStream()   获取实体内容数据

 3.5 service 和 doXX方法区别

技术分享
 1 HttpSevlet类的源码: 2 protected void service(HttpServletRequest req, HttpServletResponse resp) 3         throws ServletException, IOException { 4        //得到请求方式 5         String method = req.getMethod(); 6  7         if (method.equals(METHOD_GET)) { 8             long lastModified = getLastModified(req); 9             if (lastModified == -1) {10                 // servlet doesn‘t support if-modified-since, no reason11                 // to go through further expensive logic12                 doGet(req, resp);13             } else {14                 long ifModifiedSince;15                 try {16                     ifModifiedSince = req.getDateHeader(HEADER_IFMODSINCE);17                 } catch (IllegalArgumentException iae) {18                     // Invalid date header - proceed as if none was set19                     ifModifiedSince = -1;20                 }21                 if (ifModifiedSince < (lastModified / 1000 * 1000)) {22                     // If the servlet mod time is later, call doGet()23                     // Round down to the nearest second for a proper compare24                     // A ifModifiedSince of -1 will always be less25                     maybeSetLastModified(resp, lastModified);26                     doGet(req, resp);27                 } else {28                     resp.setStatus(HttpServletResponse.SC_NOT_MODIFIED);29                 }30             }31 32         } else if (method.equals(METHOD_HEAD)) {33             long lastModified = getLastModified(req);34             maybeSetLastModified(resp, lastModified);35             doHead(req, resp);36 37         } else if (method.equals(METHOD_POST)) {38             doPost(req, resp);39             40         } else if (method.equals(METHOD_PUT)) {41             doPut(req, resp);        42             43         } else if (method.equals(METHOD_DELETE)) {44             doDelete(req, resp);45             46         } else if (method.equals(METHOD_OPTIONS)) {47             doOptions(req,resp);48             49         } else if (method.equals(METHOD_TRACE)) {50             doTrace(req,resp);51             52         } else {53             //54             // Note that this means NO servlet supports whatever55             // method was requested, anywhere on this server.56             //57 58             String errMsg = lStrings.getString("http.method_not_implemented");59             Object[] errArgs = new Object[1];60             errArgs[0] = method;61             errMsg = MessageFormat.format(errMsg, errArgs);62             63             resp.sendError(HttpServletResponse.SC_NOT_IMPLEMENTED, errMsg);64         }65     }
View Code

 

day9http协议