http Introductionhttp The request part of http Basic structure of requestRequest lineDetailed explanation HTTP request headerHttp Response detailshttp Basic structure of responseStatus linehttp Detailed explanation of response message headerExpires, Pragma, Cache-Control Set not to cacheExpires, Pragma, Cache-Control Settings Specify cache timeHTTP Request details——General information header
http protocol is based on tcp/ip protocol
http protocol full name Hypertext Transfer Protocol ( HTTP,HyperText Transfer Protocol)
http protocol version 1.0 1.1 2.0
http 1.0 becomes a short connection, http 1.1 is called a long connection
The so-called long and short refers to the long connection lasting 1.1 30s and the short connection being disconnected immediately after sending the data
Http defines different methods for interacting with the server. There are four basic methods, namely GET, POST, PUT, and DELETE. The full name of URL is resource descriptor. We can think of it this way: a URL address, which is used to describe a resource on the network, and GET, POST, PUT, and DELETE in HTTP correspond to the search and modification of this resource. Add and delete 4 operations. At this point, everyone should have a general understanding. GET is generally used to obtain/query resource information, while POST is generally used to update resource information.
Request line
Message header
A blank line
## Content
Request lineRequest methods include: post, get, options, delete, trace, put
Commonly used ones are: post, get
The difference between post and get: GET uses URL or Cookie to pass parameters. And POST puts the data in BODY. The GET URL will have a length limit, and the POST data can be very large. POST is safer than GET because the data is not visible on the address bar. Accept: Tell the server the files I can accept Type MIME types acceptable to the browser Accept-Charset: Character set encoding acceptable to the browser Accept-Encoding: Can accept compressed data in a certain format such as: gzip, compress. The data encoding method that the browser can decode Accept-Langage: The language supported by the browser Host: Indicates who the host I am looking for is ## If-Wodified-Since: Tell the server whether there is a file to be requested in the local cache that contains the time of the requested file // The server receives this request and compares the time to determine whether the file requested by the browser has changed. If Changes will send a new file to the browser. The data will not be sent again without changes. //Note: It will only be returned if the requested content has been modified after the specified date, otherwise 304"Not will be returned.
Modified" response. Referer: Tell the server where I am from. This message header is often used to prevent hot links. My personal understanding on how to prevent hotlinking: Hotlinking: Hotlinking means that the service provider itself does not provide services Content, bypassing other beneficial end-user interfaces (such as advertisements) through technical means, directly providing service content of other service providers to end-users on their own websites, defrauding end-users’ browsing and click-through rates. No beneficiaries. Providing resources or providing few resources without any benefit to the real service provider ## The value of the referer is what you click on the connection file. Location. referer.startWith("Internal Path"); ##Connection: Indicates whether a persistent connection is required. If the Servlet sees that the value here is "Keep-Alive", or see Since the request uses HTTP 1.1 (HTTP 1.1 uses persistent connections by default), it can take advantage of persistent connections and significantly reduce the download time when the page contains multiple elements (such as Applets, images). At this point, the Servlet needs to send a Content-Length header in the response. The simplest way to implement it is: first write the content
ByteArrayOutputStream and then calculates its size before actually writing out the contents. Date: The time when the browser sent the http request. Content-Length: Indicates the length of the request message body. ##UA-Pixels, UA-Color, UA-OS, UA-CPU: Sent by some versions of IE browser Non-standard request headers indicating screen size, color depth, operating system and CPU type. ## Status line Multiple headers One blank line Entity content ##Status line Format: http version number status code reason description The status code is used to indicate the server's processing result of the request. It is a three-dimensional decimal number. Response status codes are divided into 5 categories. Location:让浏览器重新定位到 指定的 URL Server:告诉浏览器 服务器的类型 Content-Encoding:服务端能够发送压缩编码类型 Content-Length: 服务器端发送的压缩数据的长度 Content-Langage:服务端发送的语言类型 Content-Type:服务端发送的类型及采用的编码方式 Last-Modified:服务端对该资源最后的修改(更新)时间 Refresh:服务端要求浏览器在指定的时间,刷新,然后访问指定的页面路径 Content-Disposition:attachmen;filename=aaa.zip 服务端要求客户端一下载文件的方式打开该文件,即告诉浏览器有文件需要下载 Transfer-Encoding:传送数据到客户端的方式 Set-Cookie:服务端发送到客户端的暂存数据 Cache-Control:告诉浏览器如何缓存页面数据 Expires:告诉浏览器如何缓存页面数据 参数 -1 不缓存 Pragma:告诉浏览器如何缓存页面数据 Connection:维护客户端和服务端的连接关系 是否保持连接 Date:服务端响应客户端的时间
通用信息头指既能用于请求,又能用于响应的一些消息头 Cache-Control:no-cache Pragma:no-cache Connection:close/Keep-Alive Date:Tue,。。。 The above is the detailed content of In-depth analysis of HTTP protocol. For more information, please follow other related articles on the PHP Chinese website!Detailed explanation of HTTP request headers
public void doGet(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException{ // 防止乱码 response.setContentType("text/html;charset=utf-8"); // 获取输出流 PrintWriter out = response.getWriter(); // 获取用户浏览器 Referer String referer = request.getHeader("Referer"); if(referer == null || referer.startsWith("http://localhost:8080/本地内部用户web应用路径")){ response.sendRedirect("其他非真正资源网页"); return; }else{ } // 内部资源文件 。。。。。。。。。。。 }
Http response details
The basic structure of http response
Status code ##Meaning 100~199 indicates that the request was successfully received and the client is required to continue submitting. The entire processing process can be completed only with the next request 200~299 indicates that the request was successfully received and the entire processing process has been completed. Commonly used 200 ##400~ 499##300~399 ## is used to complete the request , the client needs to further refine the request, for example: the requested resource has been moved to a new address, commonly used 302, 307The client's request is incorrect. Commonly used 404 ##500~599 ## appears on the server side Error, commonly used 500 # http 响应消息头详解
Expires、Pragma、Cache-Control 设置不缓存
// 指定该页面不缓存 ie浏览器内核response.setDateHeader("Expires",-1);// 兼容设置response.setHeader("Cache-Control","no-cache");response.setHeader("Pragma","no-cache");
Expires、Pragma、Cache-Control 设置 指定缓存时间
// 指定该页面缓存指定时间 ie浏览器内核response.setDateHeader("Expires",System.currentTimeMillis()*3600*1000*24;
HTTP 请求的细节————通用信息头