Get and Post requests

Source: Internet
Author: User

Get and Post requests

1. Request Length
RFC does not limit the length of Get and Post requests.
However, the browser has restrictions. For example, the IE browser's Get request cannot exceed 2083 characters, and the Firefox browser cannot exceed 65536 characters. The browser has no restrictions on Post requests.
 
The server can restrict Get and Post requests. For example, maxHttpHeaderSize in ctor limits the length of Get requests, while maxPostSize limits the length of Post requests. The difference between the two types of requests is that the get request is in the Http Header, while the Post request is in the Http Body.
 
2. Encoding
Spring CharacterEncodingFilter and request setCharacterEncoding are only for Http bodies. These two methods do not work for Get requests.
Take Tomcat as an example. You can adjust the URIEncoding parameter or enable useBodyEncodingForURI to adjust the Get request encoding. URIEncoding is a ISO-8859-1 encoding by default.
Of course, this method is not universal and you need to modify the middleware configuration.
Another method is to encode Chinese characters twice through JS on the front-end page.
Take the following front-end page as an example to submit the Encoded chinese data to the backend Servlet.
<Html>
<Head>
<Meta http-equiv = "Content-Type" content = "text/html; charset = UTF-8">
<Script language = 'javascript '>
Function get (){
Var data = 'Chinese requests ';
Data = encodeURIComponent (data ));
Document. getElementById ("a"). href = "http: // 127.0.0.1: 8080/Web/Encoding? Name = "+ data;
}
</Script>
</Head>
<Body>
<A onClick = "get ()" id = "a"> Click <a/>
</Body>
</Html>
 
The Servlet decodes Chinese data once.
String name = URLDecoder. decode (request. getParameter ("name"), "UTF-8 ");

Take "Chinese request" as an Example
Result of the first front-end JS Code: % E4 % B8 % AD % E6 % 96% 87% E8 % AF % B7 % E6 % B1 % 82
Result of second front-end JS Code: % 25E4% 25B8% 25AD % 25E6% 2596% 2587% 25E8% 25AF % 25B7% 25E6% 25B1% 2582
After the first encoding, the data is already ASCII characters, so the second encoding is to further encode "%" in the first encoding as "% 25"
After the request arrives at Tomcat, Tomcat will automatically decode according to URIEncoding (ISO-8859-1 by default), so before entering the Servlet's doGet method, the data has been restored
"% E4 % B8 % AD % E6 % 96% 87% E8 % AF % B7 % E6 % B1 % 82"
Therefore, the correct data can be obtained by using String name = URLDecoder. decode (request. getParameter ("name"), "UTF-8.
The advantage of this method is that you do not need to modify the middleware configuration. The disadvantage is that two encodings will cause URL expansion.
 
3. cache content
The browser caches the get request results until they expire, which is also the reason why JSONP technology needs to use random numbers.

This article permanently updates the link address:

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.