[web] Get and post differences, enctype submit data in a detailed format

Source: Internet
Author: User
Tags html header http post alphanumeric characters

Reprinted from: http://www.cnblogs.com/sunxucool/archive/2012/12/11/2813113.html

1. Get is the data that is fetched from the server and post is the data sent to the server.

2. Get is the URL where the parameter data queue is added to the Action property of the submission form, and the value corresponds to the field one by one in the form, which is visible in the URL. Post is the HTTP post mechanism that places the fields within the form with their contents in the HTML header, along with the URL address referred to by the Action property. The user does not see the process.

3. For Get mode, the server side uses Request.QueryString to get the value of the variable, and for post, the server side uses Request.Form to obtain the submitted data.  4. Get transmits a small amount of data and cannot be greater than 2KB. Post transmits a large amount of data, which is generally not restricted by default. In theory, however, the maximum amount of IIS4 is 100KB in 80KB,IIS5.  5. Get security is very low and post security is high.  ----------------------------------------------in the syntax of the form element, Enctype indicates the format of the submitted data   Enctype property specifies the type of encoding that the browser uses when sending data back to the server.   Below is description:  application/x-www-form-urlencoded: The form data is encoded as a name/value pair. This is the standard encoding format.  multipart/form-data: The form data is encoded as a message, and each control on the page corresponds to a part of the message.  text/plain: Form data is encoded in plain text with no control or formatting characters. The Enctype property of the   supplement form is encoded in two common ways: application/x-www-form-urlencoded and Multipart/form-data, the default is application/ X-www-form-urlencoded.   When action is get, the browser uses x-www-form-urlencoded encoding to convert the form data into a string (Name1=value1&name2=value2 ... ), and then append the string to the URL, using the. Split, to load the new URL.   When action is post, the browser encapsulates the form data into the HTTP body and then sends it to the server.   If you don't have a type=file control, use the default application/x-www-form-urlencoded.   But if you have type=file, you'll need to use Multipart/form-data. The browser splits the entire form in controls and adds Content-disposition (form-data or file) to each section, Content-type (the default isText/plain), information such as name (control name), and with the delimiter   in the syntax of the form element, Enctype indicates that the format of the submitted data uses the Enctype property to specify the type of encoding the browser uses when sending data back to the server. Below is the description: application/x-www-form-urlencoded: The form data is encoded as a name/value pair. This is the standard encoding format. Multipart/form-data: The form data is encoded as a message, and each control on the page corresponds to a part of the message. Text/plain: Form data is encoded in plain text with no control or formatting characters.
Add
The Enctype property of a form is encoded in two common ways: application/x-www-form-urlencoded and Multipart/form-data, which are application/ X-www-form-urlencoded. When action is get, the browser uses x-www-form-urlencoded encoding to convert the form data into a string (name1=value1& amp;name2=value2 ... ), and then append the string to the URL, using the. Split, to load the new URL. When the action is post, the browser encapsulates the form data into the HTTP body and then sends it to the server. If you don't have a type=file control, you can use the default application/x-www-form-urlencoded. But if you have type=file, you will need to use Multipart/form-data. The browser splits the entire form into units of controls and adds Content-disposition (form-data or file) to each section, Content-type (default = Text/plain), name (control name), and so on. and add the separator (boundary).

Get and post differences are detailed

1, get is used to obtain data from the server, and post is used to pass data to the server.
2. Get adds the data in the form in the form of Variable=value, followed by the URL to which the action points, and both use "?". connections, and each variable is connected by a "&" connection; Post is the data in the form that is placed in the data body of the forms, in the same way that the variables and values are passed to the URL that the action points to.
3, get is not safe, because in the transmission process, the data is placed in the requested URL, and now many existing servers, proxy servers or user agents will log the request URL to the journal file, and then put in a place, so that there may be some privacy information to be seen by third parties. In addition, users can see the submitted data directly in the browser, and some system internal messages will be displayed in front of the user. All the actions of the post are not visible to the user.
4, get transmission of small amount of data, mainly because of the URL length limit, and post can transfer a large amount of data, so the upload file can only use post (of course, there is a reason, will be mentioned later).
5. Get restricts the value of the data set for form forms to be ASCII characters, while Post supports the entire ISO10646 character set. The default is to encode with iso-8859-1
6, get is the default method for form.

The following comparisons are very very much used:

Java Web Development has a period of time, there is a problem is always bothering me, is garbled problem, is basically on-line search solution (online information is really a lot), are a lot of introduction how to solve this kind of garbled problem, but not a few of the context of the problem to say clearly, sometimes read some articles, Think I understand, but in the development of garbled problems and like ghosts out of the scary, really big head! This article is my long time and garbled to fight some of the accumulation of understanding, but also hope that more friends to give guidance and supplement.
The form has 2 methods to submit data to the server, get and post, respectively.
(a) Get submitted
1. First, the client (browser) Form table with the Get method is how to encode data to the server side.

For a Get method, the data is concatenated to the requested URL as a parameter, such as: HTTP://LOCALHOST:8080/SERVLET?MSG=ABC
(A very common garbled problem is about to appear, if the URL appears in Chinese or other special characters, such as: http://localhost:8080/servlet?msg= Hangzhou, the server is easy to get garbled), URL stitching completed, the browser will URL URL Encode, and then sent to the server, the URL encode process is to make part of the URL as a character, according to some encoding (such as: UTF-8,GBK, etc.) encoded into binary bytecode, and then each byte with a 3-character string "%xy" representation, Where XY is the two-bit hexadecimal representation of the byte. What I said here may not be clear, the specific introduction can look at the introduction of the Java.net.URLEncoder class here. Understanding the process of URL encode, we can see 2 very important questions, first: the characters that need URL encode are generally non-ASCII characters (general speaking), and then popular is that in addition to the English alphabet text (such as: Chinese, Japanese, etc.) are to be URL encode, So for us, are the English letter URL will not appear the server get garbled problem, garbled is the URL with a Chinese or special character caused by; second: URL encode exactly how to encode characters in that way? This is the browser thing, and different browsers have different practices, the Chinese version of the browser will generally use the default GBK, by setting the browser can also use the UTF-8, may be different users have different browser settings, but also create different encoding method, So many of the site's approach is to first put the URL inside the Chinese or special characters with JavaScript URL encode, and then splicing the URL to submit data, that is, for the browser to do a URL encode, the advantage is that the site can be unified get method to submit data encoding method. When the URL encode is completed, the current URL becomes a character in the ASCII range and is then converted to binary with the ISO-8859-1 encoding to send it along with the request header. Here to say a few more, for the Get method, there is no request entity, the URL containing the data in the request header, the reason for the URL encode, my personal feeling is: for the request header is ultimately to be encoded with ISO-8859-1 encoding into binary 101010 ..... The pure data in the Internet transmission, if directly containing Chinese and other special characters to do iso-8859-1 encoding will be lost information, so it is necessary to do the URL encode first.
2. How the server-side (Tomcat) obtains the data to be decoded.
The first step is to decode the data with iso-8859-1, for the Get method, Tomcat gets the data is the ASCII range of the request header character, where the request URL with parameter data, if the parameters have special characters such as Chinese, then the URL is still encode after the% XY state, stop first, let's start with the process by which developers generally get data. Usually everyone is request.getparameter ("name") to obtain the parameter data, our request object or data are decoded, and the decoding process is not specified in the program, here to say, there are a lot of beginners to use Request.setcharacterencoding ("character set") can specify the decoding method, in fact, it is not possible to see the servlet's official API description has an explanation of this method: Overrides the name of the character Encoding used in the body of this request. This method must is called prior to reading request parameters or reading input using Getreader (). It can be seen that he is powerless in the way of the Get method. So exactly what encoding method to decode the data, this is Tomcat thing, default is iso-8859-1, so we can find why GET request with Chinese parameter why on the server side get garbled, because in the client is generally used UTF-8 or GBK to the data URL encode, here with iso-8859-1 way URL decoder obviously not, in the program we can directly
Java code

1. New String (Request.getparameter ("name"). GetBytes ("Iso-8859-1"), "client-specified URL encode encoding")


Restore the bytecode, and then decode the data in the correct way, the article on the Internet is usually a configuration in tomcat
XML code

1. <connector port= "8080" protocol= "http/1.1" maxthreads= "" "connectiontimeout=" 20000 "redirectport=" 8443 " uriencoding= "GBK"/>


This is where Tomcat gets the data in the specified way URL Decoder,url Decoder's introduction here


(i) Post submission
1. The Client (browser) Form form is used only by the Post method to encode the data and submit it to the server side.
The data to be transmitted in the Post method is also URL encode, so what is the encoding method?
In the HTML file where the form is located if there is a paragraph <meta http-equiv= "Content-type" content= "text/html; charset= Character Set (Gbk,utf-8, etc.) "/>, then the post will be encoded using the encoding specified here. Generally people think that this code is to let the browser know what character set to interpret the page, so the site will put it in the HTML code of the most front-end, try not to appear garbled, in fact, it also has a role is to specify the form form of the Post method to submit the data URL encode encoding method. It can be seen from here that for the Get method to count, the browser to the data URL encode encoding method is a browser settings to decide, (can be specified with JS), and the Post method, the developer can specify.
2. How the server-side (Tomcat) obtains the data to be decoded.
If you use the Tomcat default setting and do not make encoding settings such as filters, then he is also decoding with iso-8859-1, but Request.setcharacterencoding ("character set") can be useful.

What I found above is that Tomcat does not specify the encoding method in the request header, if the request header specifies the encoding method will be encoded in this way.
There are 2 articles recommended, the address is
In layman URL code: http://www.cnblogs.com/yencain/articles/1321386.html;
table when submitting data with Post method garbled problem: http://wanghuan8086.javaeye.com/blog/173869

It is important to use post in the HTML file where the form is located if there is a paragraph <meta http-equiv= "Content-type" content= "text/html; charset= Character Set (Gbk,utf-8, etc.) "/>
It is strongly recommended to use post submission
////////////////////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////////////////////////
Both get and post two methods send data to the server, but which one should you use?
The HTTP standard contains both methods for different purposes. Post is used to create the resource, and the contents of the resource are programmed into the HTTP instructions. For example, processing order forms, adding new rows of data to a database, and so on.
The Get method is used when a request has no side effects, such as a search, and the Post method is used when the request has side effects, such as adding data rows to the database. A more practical problem is that the Get method may produce a very long URL, perhaps exceeding the URL length limit for some browsers and servers.
Use the Post method if any of the following conditions are true:

The result of the request has a persistent side effect, for example, adding new rows of data within the database.
If you use the Get method, the data collected on the form may cause the URL to be too long.
The data to be transmitted is not in 7-bit ASCII encoding.

Use the Get method if any of the following conditions are true:

The request is to find the resource, and the HTML form data is used only to help search.
The result of the request has no persistent side effects.
The total length of the data collected and the input field names within the HTML form does not exceed 1024 characters.

The difference between get and post methods in form submission
1. Get is the data that is fetched from the server and post is the data sent to the server.

2. Get is the URL where the parameter data queue is added to the Action property of the submission form, and the value is corresponding to the field one by one in the form, which can be seen in the URL, so security is not high. Post is the HTTP post mechanism that places the fields within the form with their contents in the HTML header, along with the URL address referred to by the Action property. Users do not see this process, security is relatively high.

3. For Get mode, the server side uses Request.QueryString to get the value of the variable, and for post, the server side uses Request.Form to obtain the submitted data.

4. Get transmits a small amount of data, cannot be greater than 2KB. Post transmits a large amount of data, which is generally not restricted by default. In theory, however, the maximum amount of IIS4 is 100KB in 80KB,IIS5.

5. Get security is very low, post security is high.

6. The post page cannot be added to the Favorites folder, and the Get page can be added.

The Enctype attribute in the ContentType and form corresponds to the way the form is used to organize the data, consisting mainly of two types:
1, application/x-www-form-urlencoded: It is the default content type (contents type), in the results we have seen the effect of the data encoded by it. Encoding method: I. Use "+" instead of a space, non-alphanumeric characters, is used to%hh the ASCII substitution of the character (the Chinese character is this form); Ii. variables and values Use "=", and use the "&" connection between each variable and value pair.
2, Multipart/form-data: It can be used to transfer a large number of binary data or non-ASCII characters of text, so the upload file needs to be set enctype= "Multipart/form-data", this method must be post. The message it transmits contains a series of data blocks, each representing a variable in the form, and the order of the data blocks is consistent with the order of the pages, separated by special characters (boundary) between the blocks and blocks. If the form contains a file control, the corresponding data block also contains a Content-type header that specifies the MIME, and the default value is Text/plain. The following is the result of the content when setting enctype= "Multipart/form-data":

[web] Get and post differences, enctype submit data in a detailed format

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.