Get and post code--Go

Source: Internet
Author: User

1, get is used to obtain data from the server (no request body), and post is used to pass data to the server (including the request body).

2. Get adds the data in the form in the form of variable=value to the URL to which the action (service) 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, because of the URL length limit, post can transfer a large amount of data, so in 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.

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 ways to submit data to the server, get and post, respectively, saying:

Get

1. Client (browser) encoding

For a Get method, the data is concatenated to the requested URL as a parameter, such as: HTTP://LOCALHOST:8080/SERVLET?MSG=ABC. If the URL appears in Chinese or other special characters, such as: http://localhost:8080/servlet?msg= Hangzhou, the browser will URL encode URL, and then sent to the server. URL encode is the process of making part of the URL as a character, encoded in some way (such as: UTF-8,GBK, etc.) into binary bytecode, and then each byte with a 3-character string "%xy", where XY is the two-bit hexadecimal representation of the byte, Specific introduction can look at the Java.net.URLEncoder class, we can see 2 very important questions:

First: The characters that need URL encode are generally non-ASCII characters (generally speaking), and then the popular word is that in addition to the English alphabet (such as: Chinese, Japanese, etc.) are to be URL encode, so for us, is the English letter URL will not appear the server get garbled problem, garbled is the URL with a Chinese or special characters caused;

Second: URL encode exactly how to encode characters in that encoding? 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. Server-side decoding

    The first step is to decode the data with iso-8859-1, and for the Get method, Tomcat gets the data in the ASCII range of the request header character, where the request URL has parameter data , if there are special characters such as Chinese in the parameter, then it is still the%xy state after Url encode, first stop, we first say the developer generally get the data process. 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 novice   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 be called prior to reading request  parameters or reading input using getreader (). It can be seen that he is powerless for the Get method. So exactly what encoding method to decode the data, this is Tomcat thing, the default is  iso-8859-1, so we can find why get request with Chinese parameters on the server side get garbled, The reason is that in the client is generally used UTF-8 or GBK to the data  url encode, here iso-8859-1 way Url decoder obviously not, in the program we can directly:

 

[Java] view plaincopy

  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 web is usually in Tomcat to make a configuration XML code:

[HTML] view Plaincopy

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

This allows Tomcat to URL decoder in the specified manner after fetching the data

Post

1. Client (browser) encoding

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. As can be seen from here, for the Get method, URL encode is encoded by the browser settings, (can be specified with JS), and the Post method, the developer can specify.

2. Server-side decoding

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. I found out that the Tomcat thing was done in the request header without specifying the encoding, if the request header specifies that the encoding will be encoded in the way specified.

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.

Get and post code--Go

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.