Home >Web Front-end >JS Tutorial >Detailed introduction to restful http

Detailed introduction to restful http

零下一度
零下一度Original
2017-07-24 14:20:271741browse
HTTP (HyperText Transfer Protocol) is a set of rules for computers to communicate through the network. Computer experts designed HTTP to enable HTTP clients (such as Web browsers) to request information and services from HTTP servers (Web servers). The current version of the HTTP protocol is 1.1. HTTP is a stateless protocol, and stateless refers to the Web There is no need to establish a persistent connection between the browser and the Web server. This means that when a client makes a request to the server and the Web server returns a response, the connection is closed and no information about the connection is retained on the server. Information.HTTP follows the request/response model. The web browser sends a request to the web server, and the web server processes the request and returns an appropriate response. All HTTP connections are structured as a set of requests and responses.
HTTP uses content types, which means that the files returned by the web server to the web browser have related types. All of these types are modeled on the MIME Internet Mail Protocol, which means that the web server tells the web browser what kind of file it is, whether it is an HTML document, a GIF image, a sound file, or a standalone application. Most Web browsers have a set of configurable helper applications that tell the browser how to handle various content types sent by the Web server.
The HTTP communication mechanism is that during a complete HTTP communication process, the following 7 steps will be completed between the web browser and the web server:
(1) Establish a TCP connection
Before HTTP work begins, the web browser must first establish a connection with the web server through the network. The connection is completed through TCP. This protocol and the IP protocol jointly build the Internet, the famous TCP/IP protocol family, so the Internet Also known as TCP/IP network. HTTP is a higher-level application layer protocol than TCP. According to the rules, only after the lower-layer protocol is established, the higher-layer protocol can be connected. Therefore, a TCP connection must be established first. Generally, the port number of the TCP connection is 80
(2) The web browser sends a request command to the web server
Once the TCP connection is established, the web browser will send a request command to the web server
For example: GET/sample/hello .jsp HTTP/1.1
(3) The Web browser sends the request header information
After the browser sends its request command, it also sends some other information to the Web server in the form of header information. , and then the browser sends a blank line to notify the server that it has finished sending the header information.
(4) Web server response
After the client sends a request to the server, the server will send a response back to the client,
HTTP/1.1 200 OK
The first part of the response is the protocol version number and response status code
(5) The Web server sends response header information
Just as the client will send information about itself along with the request, the server will also Along with the response, the user is sent data about itself and the requested document.
(6) The Web server sends data to the browser
After the Web server sends the header information to the browser, it will send a blank line to indicate that the sending of the header information ends here, and then , it sends the actual data requested by the user in the format described by the Content-Type response header information
(7) The Web server closes the TCP connection
Under normal circumstances, once the Web server sends a request to the browser After the server sends the request data, it will close the TCP connection, and then if the browser or server adds this line of code to its header information
Connection:keep-alive
After the TCP connection is sent will remain open so the browser can continue sending requests over the same connection. Keeping connections saves the time required to establish a new connection for each request and also saves network bandwidth.
  
HTTP request format
When the browser makes a request to the Web server, it passes a data block to the server, which is the request information. The HTTP request information consists of 3 Part composition:
l Request method URI protocol/version
l Request header (Request Header)
l Request body
The following is an HTTP request Example:
GET/sample.jspHTTP/1.1
Accept:image/gif.image/jpeg,*/*
Accept-Language:zh-cn
Connection:Keep-Alive
Host:localhost
User-Agent:Mozila/4.0(compatible;MSIE5.01;Window NT5.0)
Accept- Encoding:gzip,deflate
username=jinqiao&password=1234
(1) Request method URI protocol/version
request The first line is "Method URL proposal/version": GET/sample.jsp HTTP/1.1
In the above code, "GET" represents the request method, "/sample.jsp" represents the URI, "HTTP/1.1
Represents the protocol and protocol version.
According to the HTTP standard, HTTP requests can use multiple request methods. For example: HTTP1.1 supports 7 request methods: GET, POST, HEAD, OPTIONS, PUT, DELETE and TARCE. In Internet applications, the most commonly used methods are GET and POST.
The URL completely specifies the network resource to be accessed, usually just giving a relative directory relative to the root directory of the server, so it always starts with "/", and finally, the protocol version declares the communication process version using HTTP.
(2) Request Header
The request header contains a lot of useful information about the client environment and the request body. For example, the request header can declare the language used by the browser, the length of the request body, etc.
Accept:image/gif.image/jpeg.*/*
Accept-Language:zh-cn
Connection:Keep-Alive
Host :localhost
User-Agent:Mozila/4.0(compatible:MSIE5.01:Windows NT5.0)
Accept-Encoding:gzip,deflate.
(3) Request body
There is a blank line between the request header and the request body. This line is very important. It indicates that the request header has ended, and what follows is the request body. The request body can contain the query string information submitted by the customer:
username=jinqiao&password=1234
In the above example of the HTTP request, the request body has only one line of content. Of course, in actual applications, the HTTP request body can contain more content.
HTTP request method I will only discuss the GET method and POST method here
l GET method
The GET method is the default HTTP request method. We use the GET method to submit forms every day. Data, however, the form data submitted using the GET method is only briefly encoded, and it will be sent to the Web server as part of the URL. Therefore, if you use the GET method to submit the form data, there are security risks. For example
Http://127.0.0.1/login.jsp?Name=zhangshi&Age=30&Submit=%cc%E+%BD%BB
From the above URL request, it is easy to Recognize the content of form submissions. (What follows?) In addition, since the data submitted by the GET method is part of the URL request, the amount of data submitted cannot be too large
l   POST method
The POST method is an alternative to the GET method Method, it mainly submits form data to the Web server, especially large batches of data. The POST method overcomes some of the shortcomings of the GET method. When submitting form data through the POST method, the data is not transmitted to the Web server as part of the URL request but as standard data. This overcomes the shortcomings of the GET method in which the information cannot be kept confidential and the data volume is too small. Therefore, for security reasons and respect for user privacy, the POST method is usually used when submitting forms.
From a programming perspective, if the user submits data through the GET method, the data is stored in the QUERY_STRING environment variable, while the data submitted by the POST method can be obtained from the standard input stream.
HTTP response is similar to HTTP request. HTTP response also consists of 3 parts, namely:
l Protocol status version code description
l Response header (Response Header)
l Response text
The following is an example of an HTTP response:
HTTP/1.1 200 OK
Server:Apache Tomcat/5.0.12
Date:Mon,6Oct2003 13:23:42 GMT
Content-Length:112
 
<html><head>
<title>HTTP响应示例<title>
</head>
<body>
Hello HTTP!
</body>
</html>协议状态代码描述HTTP响应的第一行类似于HTTP请求的第一行,它表示通信所用的协议是HTTP1.1服务器已经成功的处理了客户端发出的请求(200表示成功):
HTTP/1.1 200 OK响应头(Response Header)响应头也和请求头一样包含许多有用的信息,例如服务器类型、日期时间、内容类型和长度等:
   Server:Apache Tomcat/5.0.12
Date:Mon,6Oct2003 13:13:33 GMT
Content-Type:text/html
Last-Moified:Mon,6 Oct 2003 13:23:42 GMT
Content-Length:112
 响应正文响应正文就是服务器返回的HTML页面:
  <html><head>
<title>HTTP响应示例<title>
</head>
<body>
Hello HTTP!
</body>
</html>
The response header and the body must also be Separate with blank lines.​
l​​​ HTTP response code
HTTP response code is also called status code, which reflects the status of the Web server processing HTTP requests. The HTTP response code consists of 3 digits, where the first digit defines the type of response code:
1XX - Information (Information), indicating that a web browser request has been received and is being processed further
2XX-Successful, indicating that the user request was correctly received, understood and processed. For example: 200 OK
3XX-Redirection, indicating that the request was not successful and the customer must take further action .
4XX-Client Error (Client Error), indicating that the request submitted by the client has an error. For example: 404 NOT
                      Found, which means that the document referenced in the request does not exist.
5XX-Server Error (Server Error) means that the server cannot complete the processing of the request: such as 500
For us web developers, mastering HTTP response codes can help improve web application debugging efficiency and accuracy.
Secure connection
One of the most common uses of Web applications is e-commerce, which can use Web server-side programs to enable people to shop online. It needs to be pointed out that by default, sending information over the Internet is not secure. If someone happens to How bad would it be if a message you sent to a friend was intercepted and he could open it and imagine it contained your credit card number? Fortunately, many web servers and web browsers have the ability to create secure connections. , so that they can communicate safely.
The most common standard for providing secure connections over the Internet is the Secure Sockets layer (SSl) protocol. The SSL protocol is an application layer protocol (like HTTP) used to exchange data on the Web in a secure manner. SSL uses a public key encoding system. Essentially, this means that each party in the business has a public and a private key. When one party encodes using the other party's public key, only the person with the matching key can decode it. Simply put, public key encoding provides a secure method for exchanging data between two parties. After the SSL connection is established, both the client and the server exchange public keys and verify them before making business contact. Once both parties Once the keys are verified, data can be exchanged securely.
  • GET
    Get the resource by requesting the URI

  • POST,
    Used to add new content

  • PUT
    Used to modify a certain content

  • ##DELETE,

    Delete a certain content

  • CONNECT,

    is used for proxy transmission, such as using SSL

  • ##OPTIONS
  • Ask which

    methods can be executed

  • PATCH,
  • Partial document changes


  • PROPFIND, (wedav)
  • View properties


  • PROPPATCH, (wedav)
  • Set properties


  • MKCOL, (wedav)
  • Create a collection (folder)


  • COPY, (wedav)
  • Copy


  • MOVE, (wedav)
  • Move


  • LOCK, (wedav )
  • Lock


  • UNLOCK (wedav)
  • Unlock


  • TRACE
  • For remote diagnosis server


  • HEAD
  • Similar to GET, but does not return body information. It is used to check whether the object exists and get the metadata of the object


The above is the detailed content of Detailed introduction to restful http. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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