Difference between post and get in Network Programming

Source: Internet
Author: User
Tags form post

1. Get is used to obtain data from the server, while post is used to transmit data to the server.
2. Get adds the data in the form to the URL pointed to by action in the form of variable = value, and the two use "?" And each variable is connected by "&". Post puts the data in the form data body and passes the data to the URL indicated by the action according to the corresponding variables and values.
3. Get is insecure because data is stored in the request URL during transmission, nowadays, many existing servers, proxy servers, or user proxies record the request URL to a log file and place it in a certain place, so that some private information may be seen by a third party. In addition, you can directly view the submitted data in the browser. Some internal messages are displayed in front of the user. All post operations are invisible to users.
4. Get transmission has a small amount of data, mainly because it is restricted by the URL length; while post can transmit a large amount of data, so only post can be used for uploading files (of course, there is another reason, as mentioned later ).
5. Get restricts that the dataset value of form forms must be ASCII characters, while post supports the entire iso000046 character set. ISO-8859-1 encoding by default
6. Get is the default form method.
The following comparison is very useful:
I have been doing Java Web development for some time, and a problem is always bothering me, that is, garbled Code. It is basically a solution for searching on the Internet (there are a lot of online materials ), there are a lot of discussions about how to solve such Garbled text problems, but few of them have clearly explained the problem. Sometimes, after reading some articles, I thought I understood it, but the garbled problem in development is as scary as a ghost. It's really big! This article is an accumulation of some understanding of my long struggle against Garbled text. I also hope that more friends can give some advice and supplement.

In Form 2, you can submit the data to the server, get and post, respectively.

(1) Get submit

1. First, let's talk about how the Form on the client (browser) uses the get method to encode the data and submit it to the server.



The get method concatenates data after the request URL as a parameter, for example, http: // localhost: 8080/servlet? MSG = ABC

(A common garbled problem occurs. If Chinese characters or other special characters appear in the URL, for example, http: // localhost: 8080/servlet? MSG = Hangzhou, the server can easily get garbled characters). After URL splicing is complete, the browser performs URL encode on the URL and sends it to the server. The URL encode process uses some URLs as characters, encode a binary byte code according to a certain encoding method (such as UTF-8 and GBK), and then each byte is represented by a string containing three characters "% XY, XY is the two hexadecimal representation of the byte. I may not be clear about this. For more information, see the introduction of the java.net. urlencoder class. Understand
During the URL encode process, we can see two very important issues. First, the characters requiring URL encode are generally non-ASCII characters (in general ), in other words, except for English letters (such as Chinese and Japanese), URL encode is required. Therefore, URLs with English letters do not cause garbled characters on servers. garbled characters are caused by Chinese or special characters in URLs. Second, which encoding method does URL encode characters? Here is the browser thing, and different browsers have different practices, the Chinese version of the browser will usually use GBK by default, by setting the browser can also use UTF-8, different users may have different browser settings, which leads to different encoding methods. Therefore, many websites use the Chinese or special characters in the URL first.
Javascript performs URL encode and then Concatenates the URL to submit data, that is, URL encode for the browser. The advantage is that the website can submit data in a unified get method. After the URL encode is completed, the current URL becomes a character in the ASCII range, and then converted to binary in iso-8859-1 encoding mode as the request header is sent together. Here I would like to say a few more words: For the get method, there is no request entity, and the URLs containing data are in the request header. The reason why URL encode is used is: in the end, the request header is encoded as a binary 101010 in iso-8859-1 encoding ..... the pure data is transmitted on the Internet, if it directly contains Chinese characters and other special characters for iso-8859-1 encoding will lose information, so the first URL
Encode is necessary.

2. How does the server (Tomcat) Obtain and decode data.

The first step is to first decode the data with the iso-8859-1, for the get method, Tomcat obtains the data is the request header characters within the ASCII range, the request URL contains the parameter data, if the parameter contains special characters such as Chinese characters, it is still in the "% XY" status after URL encode. Stop it first. Let's talk about the general data acquisition process of developers. Generally, requests. getparameter ("name") is used to obtain parameter data. The request object or data obtained is decoded, but cannot be specified in the program during decoding, many new users use request. setcharacterencoding ("Character Set") can specify the decoding method. In fact, it is not feasible. Refer to the official API description of servlet to explain 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 (). we can see that the get method is powerless. So what encoding method to decode the data in the end, this is Tomcat thing, the default is the use of iso-8859-1, so we can find out why the GET request with Chinese parameters in the server side to get garbled, the reason is that the client is generally using UTF-8 or GBK on the data
URL encode, here using the iso-8859-1 URL decoder obviously does not work, in the program we can directly

Java code

1. New String (request. getparameter ("name"). getbytes ("iso-8859-1"), "url encode encoding method specified by the client ")

Restores the bytecode and decodes the data in the correct way. Online articles usually make configuration in Tomcat.

XML Code

1. <connector Port = "8080" protocol = "HTTP/1.1" maxthreads = "150" connectiontimeout = "20000" redirectport = "8443" uriencoding = "GBK"/>

This allows tomcat to URL decoder in the specified method after obtaining data. The URL decoder is described here.

(1) Post submission

1. The Form of the client (browser) uses the POST method to encode the data and submit it to the server.

The data to be transmitted in the POST method also needs URL encode. So what encoding method does it use?

In the HTML file where the form is located, if there are <meta http-equiv = "Content-Type" content = "text/html; charset = character set (GBK, UTF-8, etc) "/>, the post will be encoded using the encoding method specified here. Generally, we think that this code is used to let the browser know which character set is used to interpret the webpage. Therefore, the website will put it at the front end of the HTML code and try not to see garbled characters, in fact, it also serves to specify the form post method to submit data.
URL encode encoding method. From this we can see that for the get method, the encoding method of the URL encode of the data by the browser is determined by the browser settings (you can use Js for unified designation), and the post method, developers can specify this parameter.

2. How does the server (Tomcat) Obtain and decode data.

If Tomcat uses the default settings, and does not do the encoding settings such as the filter, it is also decoded with the iso-8859-1, but request. setcharacterencoding ("Character Set") can be used.

I found that the premise of Tomcat's operations 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.

Two articles are recommended. The addresses are

URL encoding: http://www.cnblogs.com/yencain/articles/1336686.html;

The form uses the POST method to submit data garbled problem: http://wanghuan8086.javaeye.com/blog/173869


If there is a <meta http-equiv = "Content-Type" content = "text/html; charset = character set (GBK, UTF-8) "/>
Post submission is strongly recommended.

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.