Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the difference between GET and POST methods?

2025-03-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

What is the difference between GET and POST methods? I believe many inexperienced people don't know what to do about it. Therefore, this paper summarizes the causes and solutions of the problem. Through this article, I hope you can solve this problem.

As a Web developer, you must know that in the interaction between the client and the server, the HTTP protocol provides us with many ways to use, such as the most well-known GET and POST methods, as well as PUT and DELETE.

Today we're going to talk about the most familiar and misused GET and POST methods, as well as how GET and POST methods are handled in Tomcat.

1 concept

HTTP specification definition

GET method

The GET method means retrieve whatever information (in the form of an entity) is identified by the Request-URI.

POST method

The POST method is used to request that the origin server accept

The entity enclosed in the request as a new subordinate of the resource identified by the Request-URI in the Request-Line.

Wikipedia definition

GET requests a representation of the specified resource.

POST submits data to be processed (e.g.from an HTML form) to the identified resource.

Through the above definition, we can briefly summarize:

The GET method is used to get resources

The POST method is used to save and update resources

(the picture is taken from a book illustrated by HTTP)

2 problem

So why are these two methods misused? here misuse refers more to the GET method. Look at the additional notes on Wikipedia in addition to the definition

GET

Note that GET should not be used for operations that cause side-effects, such as using it for taking actions in web applications. One reason for this is that

GET may be used arbitrarily by robots or crawlers, which should not need to

Consider the side effects that a request should cause.

POST

The data is included in the body of the request. This may result in the creation

Of a new resource or the updates of existing resources or both.

In other words, the GET method should not be used to do operations that can cause side effects on the server. POST is mainly used to create or update resource information.

In addition, another name for the GET method is the idempotent method.

The so-called idempotent means that no matter how many times it is operated, the result is the same. The POST method is naturally not idempotent because it submits data to the server every time, which is also a matter of paying special attention to the problem of repeated submission of the form.

However, in development, the GET method is widely used by developers regardless of the circumstances. Such as submitting data, getting information, deleting, and so on.

However, most application servers and Web Server implementations provide a mechanism for recording accesslog.

What will this accesslog remember? The following is an access record for the GET method in tomcat, and we find that the additional parameters in the request are also encoded.

[07/Jan/2016:11:40:50 + 0800] "GET / test/servlet?abc=%E4%BD%A0%E5%A5%BD HTTP/1.1" 200 117

While the POST method, log records only the request path.

[07/Jan/2016:11:09:11 + 0800] "POST / test/servlet HTTP/1.1" 200 127

In the application, these access logs may be provided to the third-party app for statistical analysis. Imagine that if the submitted data contains key information such as user name and password, you will know what to use when comparing the two methods.

three

Contrast

Some characteristics of GET requests:

GET request will have cache

GET requests remain in the browsing history

GET requests can be saved to bookmarks

GET requests should not be used to process sensitive data

GET request has a length limit

GET requests should only be used to get data

Some characteristics of POST requests:

POST won't have cache.

POST requests do not appear in the browser's browsing history

POST request cannot be saved to bookmark

POST is also limited in length (different Web Server may have different implementations)

four

Parameter processing

So how does parameter parsing handle these two different methods in Tomcat? It's still our constant style, talk is cheap, look at the code!

Protected void parseParameters () {

Parameters parameters = coyoteRequest.getParameters ()

Parameters.setLimit (getConnector () .getMaxParameterCount ()); / / the number of parameters is also limited. The default is 10000.

Parameters.handleQueryParameters (); / / if you submit using POST, the data cannot be parsed here

If (! getConnector () .isParseBodyMethod (getMethod () {/ / determine whether the method is POST here

Return;}

String contentType = getContentType ()

If ("multipart/form-data" .equals (contentType)) {

ParseParts (false); / / File upload is handled here

Return;}

If (! ("application/x-www-form-urlencoded" .equals (contentType) {/ / contentType submitted by form

Return;}

Int len = getContentLength ()

If (len > 0) {

Int maxPostSize = connector.getMaxPostSize ()

If ((maxPostSize > 0) & & (len > maxPostSize)) {/ / Note that there is also a length limit for POST. It is not generally said that POST has no limit. The default is 2097152.

Return;}

If (readPostBody (formData, len)! = len) {

Return;}

Parameters.processParameters (formData, 0, len); / / the code here was analyzed in the previous article

} else if ("chunked" .equals IgnoreCase (

CoyoteRequest.getHeader ("transfer-encoding")) {

FormData = readChunkedPostBody ();}

If (formData! = null) {

Parameters.processParameters (formData, 0, formData.length)

}}}

In the above code, some comments have been added to the key processing part, and all the extraneous code has been deleted.

After reading the above, have you mastered the difference between GET and POST methods? If you want to learn more skills or want to know more about it, you are welcome to follow the industry information channel, thank you for reading!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report