http protocol work process

In the TCP / IP model diagram, the reader can see, HTTP protocol located at the top of the application layer, it is the Internet's most widely used network protocol, all www documents must comply with this agreement.

 

       HTTP requests and responses is a composition, a standard client / server model (B / S configuration). HTTP protocol is always initiated by the client request, the server gives a response, as shown in FIG.

20180115150729.jpg


       HTTP is a stateless protocol. Stateless means without establishing permanent connection, a client initiates requests between client and server, the server returns a response, the connection will be closed, does not retain the server For information about the request.

HTTP works as follows:

       1. Address Resolution
       HTTP protocol is a standard URL request to the server specified in the service of a specified target URL as follows:

http://www.baidu.com:80/index.html?name=tom&age=18

       Let's resolve what URL, and see what these components are made:
       (1) HTTP: This refers to the type of protocol is to send what protocols can also be FTP and other protocols where the request is a server. page, so using a common HTTP protocol.
       (2) www.baidu.com :. hostname by the hostname can be precisely targeted to that server to be accessed in front of that network communication, IP Yes. It can be uniquely expressed server address, but IP cumbersome and complex, difficult to remember, so people thought of a way to express the address of a server through familiar English, numbers, etc., called domain names. This requires a file (as a data warehouse) and the IP domain name tie it at an early age, we really do, but as more and more IP, the file becomes more and more , bear weight. so people think of the one-relationship of these are placed on a single server, this server is called DNS domain name resolution system, it will resolve the domain name into the corresponding IP.
       (3) 80: port number of users can have access to a server through the domain name or IP, but a server, there are so many services and applications, how to find the exact service or application that users need to access it.? In the server, each of the services and applications will open a process, there will be a process ID (PID), if the external services, then there will be a unique port number, which allows external applications can be accessed directly through this port number specific services and applications. Range of port numbers is 0 to 65535, some common services and applications have default port number, generally can not be easily changed, such as port 80 Web server, 22 remote port, the database connection MySQL SSH service port 3306, etc. . Since port 80 is the default port of the Web server, so the URL HTTP request in writing when port 80 is generally omitted.
       (4) index.html:. Requested file users through the domain name and port number have access to the Web server, you can then access the file specified by the file name of the Web server are generally well route, different. in the form of access to the file of the route provided may be different, but the core is the same.
       (5) name = tom & Age = 18:?. request parameter even though the same page, it may be for different users, server to be returned to the client information is not the same, while the server is through the URL "?" different carried back parameters to respond to different users or different requests for the same user. FIG.
2. package HTTP request
       this step to write the URL above as well as the machine Some information into an HTTP request packet
3. TCP packet encapsulated
       third step is encapsulated TCP packet, TCP connection is established, it is often said that the "three-way handshake." Since HTTP application of the uppermost layer, prior to operation HTTP to establish a network connection TCP and IP protocols, this is the TCP / IP protocol suite, so the Internet is also known as TCP / IP network.
       here on the "three-way handshake" less TCP / IP protocol, After the server receives syn after packet, a return to the client to confirm the syn packet and ack packets, the client receives the last; first client sends a request to establish a connection, the client sends a syn packet, the server waits for a response ACK packet is transmitted to the server, connection will be established, as shown in FIG after sending

TCP / IP three-way handshake


4 . 客户端发送请求命令
       第四步就是在连接建立之后 , 客户端发送 HTTP 请求到服务端与请求相关的信息都会包含在请求头和请求体中发送给服务器端 .
5 . 服务器端响应
       服务器端在收到请求之后 , 根据客户端的请求发送给客户端相应的信息 , 相关的响应信息都会放在响应头和响应体中 .

 

6.关闭连接
       服务器端在发送完响应之后 , 就会关闭连接 , 如果过客户端的请求的头部信息中有 Connection-alive , 那么客户端在响应完这个请求之后不会关闭连接 , 知道客户端的所有请求都响应完毕 , 才会关闭连接 , 这样大大节省了带宽和 IO 资源 .

请求和响应

       HTTP请求室友两部分组成 : HTTP消息头和 HTTP消息体 . 消息头告诉服务器该请求是做什么的 , 消息体高速服务器怎么做 , 比如访问一个网页 , 头部信息可以到浏览器中的调试中心看 , 而消息体需要用户单机鼠标右键查看源码 , 那些HTML代码就是服务器返回给客户端的消息体 .

 

       HTTP的请求头由三部分组成 : 请求行 , 请求头和请求正文 , 如下图 :

image

  1. 请求行
           请求的第一行是请求行 , 里面有请求方法 , URL , 协议版本等 , 比如上图中 , 请求的方法是 GET , 请求的URL是/ , 协议版本是HTTP/1.1 .
           常见的请求方式有GET 和 POST , GET 方式主要用于请求网络资源 , POST 方式主要用于表单提交 , 由于GET方式的参数是在地址栏中的 , 所以总是可见的 , 不是很安全 , 而且长度也有限制 (2048个字符) , 而 POST 方式的参数是封装成实体之后发送给服务器的 , 是不可见的 , 相对比较安全 , 用户的敏感信息一般采用post 方式提交 .
  2. 请求头
           每个头域都由一个头域名 , 冒号和值域组成 , 下面介绍一些最常见的头域 :
           (1) Connection : 表示是否需要持久连接 , 如果服务器看到它的值为 keep-alive , 或者请求协议使用的是HTTP/1.1(默认使用持久连接) , 同一个页面如果包含多个资源 , 则只会使用一个连接 , 如Connection : keep-alive . 如果设置了 Connetion:close , 则每一个请求结束都会关闭连接 , 新的请求又会重新建立连接 , 一个网页至少有几十个资源请求 , 这样会浪费带宽和时间 .
           (2) Host : 这个是必需的 , 表示请求的服务器地址是什么 , 是从URL 中提取出来的 . 比如 http://www.baidu.com/ 的host 就是 www.baidu.com , 这里是80端口 , 默认省略 ; 如果是其他端口 , 比如 http://www.baidu.com:8080 , 则Host是 www.baidu.com:8080 .
           (3) Accept : 浏览器可以接受的媒体类型(MIME) , 如 Accept:text/html 代表可以接受HTML文档 , " * "代表接受任何类型 , 如 Accept:/ .
           (4) Accept-Encoding : 浏览器申明自己接受的编码方法 , 通常指定压缩方法 , 是否支持压缩 , 支持什么格式的压缩 .
  3. 请求正文
           也叫请求数据 , 在使用post请求表单数据的时候 , 这些表单数据就会被放在 HTTP 请求的请求正文中 , 以加密的形式向服务器传输 .




链接:https://www.jianshu.com/p/9f0636c5e416
 

Guess you like

Origin blog.csdn.net/XTY00/article/details/93487376