[get/post, synchronous/asynchronous, blocking/non-blocking]

When it comes to get and post requests, many students will think of synchronous and asynchronous. In fact, get and post requests belong to the requests in the http protocol. They are not directly related to synchronous and asynchronous. What are often compared with synchronous and asynchronous are blocking and non-blocking. Blocking requests. This article will elaborate on their respective characteristics and differences.


1. Common HTTP request methods

The HTTP protocol defines different methods of interacting with the server, of which the most basic five are: GET, POST, PUT, DELETE, HEAD. Among them, GET and HEAD are called safe methods, because the HTTP request using GET and HEAD will not produce any modification action on the server, and no action will be generated, which means that the HTTP request of GET and HEAD will not produce any results on the server. However, the security method does not mean that no action is generated. The security method here only means that the server information will not be modified.

The following table lists some other HTTP request methods:

method

describe

HEAD

Same as GET, but returns only the HTTP headers, not the document body.

GET

Request data from the specified resource

POST

Submit data to be processed to the specified resource

PUT

Upload the specified URI representation.

DELETE

Delete the specified resource.

OPTIONS

Returns the HTTP methods supported by the server.

CONNECT

Convert the request connection to a transparent TCP/IP channel.

Among them, the differences between GET and POST are the most frequently asked questions in interviews:

The get and post methods in the Form correspond to the GET and POST methods in the HTTP protocol during data transmission. The main differences between the two are as follows:

  • 1. Get is used to obtain data from the server, and Post is used to transfer data to the server.
  • 2. Get adds the data in the form to the URL pointed to by the action in the form of variable=value, and uses "?" to connect the two, and uses "&" to connect each variable; Post uses the form's The data is placed in the data body of the form, and is passed to the URL pointed to by the action according to the way the variables correspond to the values.
  • // get请求:请求数据在url后拼接
    xmlhttp.open(“GET”,“Demo.html?name=”+name,true);
    // post请求:请求数据放在send()中,即请求体中
    xmlhttp.open(“POST”,“Test.html”,true);
    xmlhttp.send(“name=Henry&lname=Ford”);

  • 3. Get is not safe, because during the transmission process, the data is placed in the URL of the request, and many existing servers, proxy servers or user agents will record the request URL in a log file, and then put it in a certain In this way, some private information may be seen by third parties. In addition, the user can also directly see the submitted data on the browser, and some internal system information will be displayed in front of the user. All Post operations are invisible to the user.
  • 4. The amount of data transmitted by Get is small, which is mainly due to the limitation of the URL length; while Post can transmit a large amount of data, so you can only use Post when uploading files (of course there is another reason, which will be mentioned later).
  • 5. Get restricts the value of the data set of the Form form to be ASCII characters; while Post supports the entire ISO10646 character set.
  • 6. Get is the default method of Form.

The data transmitted by Post can be converted into Chinese correctly by setting the encoding; while the data transmitted by Get has not changed. In future programs, we must pay attention to this point.

Although POST is relatively safe, the account password is still in clear text on the web page, which is very easy to steal, so it is best not to transmit private information using POST. Let me give an example with my own web system:

2. Blocking and non-blocking

Take the classic model of reading files as an example. (For the operating system, all input and output devices will be abstracted into files.)

When initiating a request to read a file, the application layer will call the I/O interface of the system kernel.

If the application layer calls blocking I/O, then after the call, the application layer is suspended immediately, waiting for the data to return, until the system kernel reads the data from the disk and returns to the application layer, the application layer Use the obtained data to perform other operations.

If the application layer calls non-blocking I/O, the system kernel will return immediately after the call (although there is no data of the file content), the application layer will not be suspended, and it can do any other operations it wants to do. (As for how the file content data is returned to the application layer, this is beyond the distinction between blocking and non-blocking.)

This is the difference between blocking and non-blocking (after breaking away from synchronous and asynchronous). To sum up, whether it is blocking or non-blocking, the focus is on the state when waiting for data to return after the interface call (sending a request). Those that are suspended and unable to perform other operations are blocking, and those that can be "extracted" immediately to complete other "tasks" are non-blocking.

3. Synchronous and asynchronous

Blocking and non-blocking solve the state problem when the application layer is waiting for data to return, so how does the data obtained by the system kernel return to the application layer? The different types of operations here reflect the difference between synchronous and asynchronous.

For synchronous calls, the application layer needs to query the system kernel by itself. If the data has not been read, the task of reading the file has not been completed at this time. The application layer will either block or suspend the file according to its blocking and non-blocking division. Or do other things (so synchronous and asynchronous do not determine the state when it is waiting for the data to return); if the data has been read, the system kernel will return the data to the application layer at this time, and the application layer can use the obtained data to do other related things.

For asynchronous calls, the application layer does not need to actively inquire about the system kernel. After the system kernel has read the file data, it will actively notify the application layer that the data has been read. At this time, the application layer can receive the returned data from the system kernel. data, and do other things.

This is the difference between synchronous and asynchronous (after breaking away from blocking and non-blocking). In other words, whether it is synchronous or asynchronous, the focus is on the way the message is notified when the task is completed. The method of blindly and actively inquiring by the caller is synchronous call, and the method of actively notifying the caller that the task has been completed by the callee is asynchronous call.


4. Summary

Tip: Here is a summary of the article:
For example: the above is what I will talk about today. This article only briefly introduces the use of pandas, and pandas provides a large number of functions and methods that allow us to process data quickly and easily.

So far, the concepts of blocking and non-blocking, synchronous and asynchronous have been clarified. Finally, here is a summary of the above, explaining what an asynchronous and non-blocking model looks like:

To be complete, the most efficient and ideal file reading asynchronous non-blocking model should be like this: the system kernel returns immediately after the application layer initiates the call (there is no file content data), and the application layer continues to do other irrelevant things. After the kernel reads the data from the disk, it actively notifies the application layer that the task has been completed. The application layer receives the data returned by the system kernel at this time, and then continues to do other related or unrelated things.

Article citation:

Cainiao Tutorial HTTP Request

Fully understand synchronous/asynchronous and blocking/non-blocking

The difference between synchronous and asynchronous and get and post request methods

Guess you like

Origin blog.csdn.net/Your_Boy_Tt/article/details/129999539