Post and GET request methods and differences

Source: Internet
Author: User

1. Some boring concepts:


HTTP defines different methods for interaction with the server. There are four basic methods: Get, post, put, and delete.

The full name of a URL is a resource descriptor. We can think that a URL address is used to describe resources on a network, while get, post, put, delete corresponds to the query, modify, add, and delete operations on this resource. Get is generally used to obtain/query resource information, while post is generally used to update resource information.


2. Get is idempotent, and post is to be modified and updated.


Through understanding the above concepts, we can easily find that get is used for information acquisition or query, which means it is idempotent (for an interpretation of idempotence, see the http://www.yining.org/2010/05/04/http-get-vs-post-and-thoughts ), post may change the resource requests on the server.


This is a bit abstract. There is an example on the Internet that I think is more reasonable: the example is as follows: Get method-the front pages of news sites are constantly updated. Although the second request will return a different batch of news, this operation is still considered idempotent because it always returns the current news. Basically, if the target is to open a link, the user can be sure that the resource has not changed from his own perspective; POST method-readers should post their comments on news, because the Site Resources are different after the comments are submitted, or the resources are modified. Consider this example carefully to understand that get is idempotent and post is to be modified and updated.


3. Understand the HTTP request process of Post and get through an instance


Through the above analysis, I have already understood get and post, and I can also feel that the difference between them is that one is to use it, and the other is to update and modify it.


Next we will use httplook to see how they make htpp requests and how they are transmitted.


First, create two JSP pages for testing:


The first page is post_get_jsp (the Code is as follows ):


<% @ Page Language = "Java" contenttype = "text/html; charset = gb18030" <br/> pageencoding = "gb18030" %> <br/> <! Doctype HTML public "-// W3C // dtd html 4.01 transitional // en" "http://www.w3.org/TR/html4/loose.dtd"> <br/> <HTML> <br/> <pead> <br/> <meta http-equiv = "Content-Type" content = "text/html; charset = gb18030 "> <br/> <title> insert title here </title> <br/> </pead> </P> <p> <body> <br/> <form name = "aaform" Action = "http: // localhost: 8080/post_get/test. JSP "method =" get "> <br/> <input type =" text "id =" text1 "name =" text1 "value =" dadfdf "> <br/> <input type = "Submit" id = "submit1" name = "submit2" value = "Submit"> <br/> </form> <br/> </body> <br /> </ptml>

The second page is test. jsp (the Code is as follows ):


<% @ Page Language = "Java" contenttype = "text/html; charset = gb18030" <br/> pageencoding = "gb18030" %> <br/> <! Doctype HTML public "-// W3C // dtd html 4.01 transitional // en" "http://www.w3.org/TR/html4/loose.dtd"> </P> <p> <% <br/> string a = request. getparameter ("text1"); <br/> out. println (); <br/> %> </P> <p> <HTML> <br/> <pead> <br/> <meta http-equiv = "Content-Type" content = "text/html; charset = gb18030 "> <br/> <title> insert title here </title> <br/> </pead> <br/> <body> </P> <p> <p> ddfdgdg </p> </P> <p> </body> <br/> </ptml>

Simply put, the HTTP request:


<Request line>

<Headers>

<Blank line>

<Request-body>

In an HTTP request, the first line must be a request line to describe the request type, resources to be accessed, and the HTTP Version Used. Next is a header section, which describes additional information to be used by the server. After the header is a blank line, you can add any other data.

 

Start testing post submission

 

The HTTP request is as follows:




Start testing get submission


The HTTP request is as follows:




4. Difference between post and get


①. Get is generally used to obtain/query resource information, while post is generally used to update resource information.


②. From the preceding HTTP request, we can see that get is used to append the request data to the URL? Separate and connect parameters. And the string has been encrypted. The post submission is put in the http package. From this point of view, the address bar of the GET request will change. rul attaches the requested data, while the post address bar will not change, and the utl will not change. A deeper understanding of the security of get is not high, so it is easy to expose information.


③. In addition, although the HTTP protocol does not limit the size of transmitted data or the URL length, the browser actually limits the URL, in this way, the size of the data transmitted by get is limited with the URL length. In contrast, there is no limit on post because it does not pass values through URLs.

 

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.