Detailed explanation of the difference between get and post


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 in the form of variable = value to the URL pointed to by the action, and uses “? For both Use “&” to connect variables; Post is to put the data in the form into the data body of the form and pass it to the URL pointed to by the action according to the corresponding way of variables and values.
3. Get is not safe because in the transmission process, the data is placed in the requested URL. Nowadays, many existing servers, proxy servers or user agents record the requested URL in the log file and place it somewhere. In this way, some private information may be seen by a third party. In addition, users can also directly see the submitted data on the browser, and some system internal messages will be displayed in front of users. All post operations are invisible to users.
4. The amount of data transmitted by get is small, which is mainly due to the limitation of URL length; 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 that the value of the data set of the form form must be ASCII characters; Post supports the entire ISO10646 character set. The default code is iso-8859-1
6. Get is the default method for form.
The following comparison is very useful:
I’ve been doing Java Web Development for a long time. There’s a problem that always bothers me. It’s basically finding solutions online (there are really many online materials). There are a lot of introductions on how to solve this kind of garbled problem, but few of them make clear the origin and context of the problem. Sometimes after reading some articles, I think I understand it, But in the development, the problem of garbled code comes out like a ghost. It’s really big! This article is the accumulation of some understanding of my long struggle with garbled code. I hope more friends will give advice and supplement.
The form has two methods to submit data to the server, get and post, respectively.
(1) Get submit
  1. First, let’s talk about how the client (browser) form uses the get method to encode the data and submit it to the server.
For the get method, the data is concatenated after the requested URL as a parameter, such as: http://localhost:8080/servlet?msg=abc
(a common garbled code problem is about to appear. If Chinese or other special characters appear in the URL, such as: http://localhost:8080 /servlet? MSG = Hangzhou, it is easy for the server to get garbled code). After the URL splicing is completed, the browser will encode the URL and send it to the server. The process of URL encoding is to take some URLs as characters and encode them into binary bytecode according to some coding method (such as UTF-8, GBK, etc.), and then each byte is represented by a string “% XY” containing 3 characters, Where XY is the two digit hexadecimal representation of the byte. What I said here may not be clear. For details, please see Java net. The urlencoder class is introduced here. After understanding the process of URL encoding, we can see two very important problems. First, the characters that need URL encoding are generally non ASCII characters (generally speaking), and then generally speaking, all words except English letters (such as Chinese, Japanese, etc.) need URL encoding, so for us, For URLs that are all English letters, there will be no problem that the server gets garbled codes. Garbled codes are caused by Chinese or special characters in the URL; Second:Which encoding method does URL encode characters in the end? This is about browsers, and different browsers have different practices. Chinese browsers generally use GBK by default. You can also use UTF-8 by setting up browsers. Different users may have different browser settings, resulting in different coding methods, Therefore, many websites first use JavaScript to encode the Chinese or special characters in the URL, and then splice the URL to submit data, that is, do URL encode for the browser. The advantage is that the website can unify the coding method of data submission by get method.After completing the URL encode, the current URL will become a character within the ASCII range, and then it will be converted into binary in the encoding mode of iso-8859-1 and sent together with the request header. What I want to say here is that for the get method, there is no request entity, and the URLs containing data are all in the request header. The reason why URL encode is used,I personally think the reason is: for the request header, it is ultimately encoded into binary 101010 in the iso-8859-1 encoding mode Pure data is transmitted on the Internet. If you directly encode iso-8859-1 containing Chinese and other special characters, you will lose information, so it is necessary to do URL encode first.
   2。 How the server side (Tomcat) obtains the data for decoding.
The first step is to decode the data with iso-8859-1. For the get method, Tomcat obtains the data from the request header characters within the ASCII range, in which the request URL contains parameter data. If there are Chinese and other special characters in the parameters, it is still in the% XY state after URL encode. Stop first. Let’s talk about the general data acquisition process of developers. Usually everyone is a request Getparameter (“name”) gets the parameter data. The data we get in the request object or are decoded, but the program cannot specify it in the decoding process. Here, many novices use itrequest. SetCharacterEncoding (“character set”) can specify the decoding method, but it is not, see the official API description of the servlet for an explanation of this method: overrides the name of the character encoding used in the body of this request This method must be called prior to reading request parameters or reading input using getReader(). It can be seen that there is nothing he can do about the get method. So what encoding method is used to decode data? This is a matter of Tomcat. By default, iso-8859-1 is used, so we can find out why the get request contains Chinese parameters and why it gets garbled code on the server. The reason is that the client usually encodes the data URL with UTF-8 or GBK. It is obviously not possible to use iso-8859-1 URL decoder here, In the program, we can directly
Java code
1. new String(request.getParameter(“name”). GetBytes (“iso-8859-1”), “URL encoding method specified by the client”)
Restore the bytecode and decode the data in the correct way. Articles on the Internet are usually configured in Tomcat
XML code
1. <Connector port=”8080″ protocol=”HTTP/1.1″ maxThreads=”150″ connectionTimeout=”20000″ redirectPort=”8443″ URIEncoding=”GBK”/> 
This allows Tomcat to use the specified URL decoder after obtaining the data. The introduction of URL decoder is here
(1) Post submission
1. How the form form of the client (browser) uses the post method to encode the data and submit it to the server.
In the post method, the data to be transmitted should also be URL encoded. What encoding method does it use?
If there is a segment in the HTML file where the form is located< meta http equiv = “content type” content = “text / HTML; charset = character set (GBK, UTF-8, etc.) / >, then post will be encoded with the encoding method specified here.It is generally believed that this code is to let the browser know what character set to interpret the web page, so the website will put it at the front of the HTML code and try not to appear garbled code. In fact, it has another functionSpecify the URL encode encoding method of the data submitted by the post method of the form。 It can be seen from here that for the get method, the encoding method of the browser’s URL encode for the data is determined by the browser settings (which can be uniformly specified with JS), while the post method can be specified by the developer.
2。 How the server side (Tomcat) obtains the data for decoding.
If Tomcat is used as the default setting and no encoding settings such as filter are made, it is also decoded with iso-8859-1, but request SetCharacterEncoding (“character set”) can come in handy.

I found that the premise of what Tomcat does above is that the encoding method is not specified in the request header. If the encoding method is specified in the request header, it will be encoded in this way.
There are 2 articles recommended. The addresses are
Simple URL Code:
Garbled code when submitting data with post method:

It is important to use post. If there is a segment < meta http equiv = “content type” content = “text / HTML; charset = character set (GBK, UTF-8, etc.) / >
Post submission is strongly recommended

Recommended Today

Vue2 technology finishing 3 – Advanced chapter – update completed

3. Advanced chapter preface Links to basic chapters: Link to component development: 3.1. Custom events of components 3.1.1. Binding custom events There are two implementation methods here: one is to use v-on with vuecomponent$ Emit implementation [PS: this method is a little similar to passing from child to parent]; The other is to use ref […]