Home > Web Front-end > JS Tutorial > In-depth analysis of HTTP protocol

In-depth analysis of HTTP protocol

一个新手
Release: 2017-09-21 10:36:40
Original
2405 people have browsed it

In-depth analysis of HTTP protocol

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 introduction

  1. http protocol is based on tcp/ip protocol

  2. http protocol full name Hypertext Transfer Protocol ( HTTP,HyperText Transfer Protocol)

  3. http protocol version 1.0 1.1 2.0

  4. 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

  5. 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.

#The request part of http

Basic structure of http request

Request line

Message header

A blank line

## Content

Request line

Request 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.

Detailed explanation of HTTP request headers
  1. Accept: Tell the server the files I can accept Type MIME types acceptable to the browser

  2. Accept-Charset: Character set encoding acceptable to the browser

  3. Accept-Encoding: Can accept compressed data in a certain format such as: gzip, compress. The data encoding method that the browser can decode

  4. Accept-Langage: The language supported by the browser

  5. Host: Indicates who the host I am looking for is

  6. ## 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.

  7. 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");

    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{                }      // 内部资源文件        。。。。。。。。。。。  }
    Copy after login

  8. #User-Agent: Tell the server, browser kernel

  9. #Cookie: This is the most important request header information. One
  10. ##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.
  11. Date: The time when the browser sent the http request.
  12. Content-Length: Indicates the length of the request message body.
  13. ##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.

Http response details

The basic structure of http response

## Status line

Multiple headers

One blank line

Entity content

##Status line

Format: http version number status code reason description

# Example : HTTP/1.1 200 OK

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.

## 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, 307##400~ 499##500~599
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
##300~399
The client's request is incorrect. Commonly used 404
## appears on the server side Error, commonly used 500#

http 响应消息头详解
  1. Location:让浏览器重新定位到 指定的 URL

  2. Server:告诉浏览器 服务器的类型

  3. Content-Encoding:服务端能够发送压缩编码类型

  4. Content-Length: 服务器端发送的压缩数据的长度

  5. Content-Langage:服务端发送的语言类型

  6. Content-Type:服务端发送的类型及采用的编码方式

  7. Last-Modified:服务端对该资源最后的修改(更新)时间

  8. Refresh:服务端要求浏览器在指定的时间,刷新,然后访问指定的页面路径

  9. Content-Disposition:attachmen;filename=aaa.zip 服务端要求客户端一下载文件的方式打开该文件,即告诉浏览器有文件需要下载

  10. Transfer-Encoding:传送数据到客户端的方式

  11. Set-Cookie:服务端发送到客户端的暂存数据

  12. Cache-Control:告诉浏览器如何缓存页面数据

  13. Expires:告诉浏览器如何缓存页面数据 参数 -1 不缓存

  14. Pragma:告诉浏览器如何缓存页面数据

  15. Connection:维护客户端和服务端的连接关系 是否保持连接

  16. Date:服务端响应客户端的时间

Expires、Pragma、Cache-Control 设置不缓存
// 指定该页面不缓存   ie浏览器内核response.setDateHeader("Expires",-1);// 兼容设置response.setHeader("Cache-Control","no-cache");response.setHeader("Pragma","no-cache");
Copy after login

Expires、Pragma、Cache-Control 设置 指定缓存时间
// 指定该页面缓存指定时间   ie浏览器内核response.setDateHeader("Expires",System.currentTimeMillis()*3600*1000*24;
Copy after login

HTTP 请求的细节————通用信息头

通用信息头指既能用于请求,又能用于响应的一些消息头

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!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template