HTTP Protocol supports many methods to retrieve data from the server or perform any operation on the server, like upload data, delete the file, etc. In total, the
HTTP protocol supports the following methods, like GET, POST, PUT, DELETE, HEAD, DELETE, OPTIONS, and TRACE, and HTTP 1.1 reserves technique called CONNECT for future use. GET, and POST is two of the most common HTTP methods you would hear or work on the web.
Though both can be used to send and receive data from client to server, there are some crucial differences between the GET and POST in HTTP, which will help you to understand when you should use GET vs. POST while writing your client and server application(尽管POST和GET都可以在客户端和服务器端之间进行收发数据,但是在HTTP中POST和GET存在着重要的区别,这些区别将有助于你理解什么时候使用POST,什么时候使用GET).
HTTP is also programming language independent;(HTTP是独立于语言的) it doesn't matter whether your client and server are written in Java or client written in HTML, JavaScript, and Server in Java, or client and server both written in .NET, you will use the HTTP protocol. (无论使用什么语言,你都会用到HTTP协议)
In this article, we will learn the pros and cons of the GET and POST method to choose which method you should use in HTML forms, considering facts like security, speed, and amount of data to transfer.(在这篇文章中,我们将会从 安全性、速度、数据传输量 方面对比学习GET和POST的优缺点,以便帮助我们在具体的情况下选择合适的请求方式)
GET /home.html HTTP/1.1
User-Agent: Mozilla/4.0 (compatible; MSIE5.01; Windows NT)
Host: www.java67.blogspot.com
Accept-Language: en-us
Accept-Encoding: gzip, deflate
Connection: Keep-Alive
here is how a POST request look like
The following is an example POST request to get the status of a job in AWS.
POST / HTTP/1.1 请求类型
content-type:application/x-www-form-urlencoded;charset=utf-8
host: https://importexport.amazonaws.com
content-length:207
上面3行为 请求头里面包含请求的终端地址
Action=GetStatus&SignatureMethod=HmacSHA256&JobId=JOBID&SignatureVersion=2&Version=2010-06-03&Signature=%2FVfkltRBOoSUi1sWxRzN8rw%3D&Timestamp=2011-06-20T22%3A30%3A59.556Z 这是请求体,请求的参数就是在这里,多个参数使用&分割
The first line represents the type of http request.
Lines 2-4 contain the HTTP headers, including the endpoint of the request.
After the HTTP headers, the body of the request contains the list of parameters. Each parameter is separated by an ampersand (&). The Action parameter indicates the action to perform For a list of the actions, see Actions.
HTTP is also programming language independent;(HTTP是独立于语言的) it doesn't matter whether your client and server are written in Java or client written in HTML, JavaScript, and Server in Java, or client and server both written in .NET, you will use the HTTP protocol. (无论使用什么语言,你都会用到HTTP协议)
In this article, we will learn the pros and cons of the GET and POST method to choose which method you should use in HTML forms, considering facts like security, speed, and amount of data to transfer.(在这篇文章中,我们将会从 安全性、速度、数据传输量 方面对比学习GET和POST的优缺点,以便帮助我们在具体的情况下选择合适的请求方式)
HTTP Method Descriptions
Before we look into the difference between GET and POST method, let's see what does each of these 8 HTTP methods does. This will set you up to understand the subtle difference between the GET vs. POST later.1. GET
The first HTTP method we will see is the GET method is used to retrieve information from the given server using a given URI. Requests using GET should only retrieve data and should have no other effect on the data. A GET request retrieves data from a web server by specifying parameters in the URL portion of the request. This is the main method used for static document retrieval.(GET方法用于通过给定的URI从给定的服务器端获取信息,使用GET请求最好只获取数据不要对数据有其他任何操作,如:更新、删除、新增等会对数据造成影响的操作。GET请求的参数是放在URI中的,这就导致了GET请求传递的参数数据量有限制,因为URI的长度是有限制的。GET是静态文档检索的要方法) Here is an example of an HTTP GET request :GET /home.html HTTP/1.1
User-Agent: Mozilla/4.0 (compatible; MSIE5.01; Windows NT)
Host: www.java67.blogspot.com
Accept-Language: en-us
Accept-Encoding: gzip, deflate
Connection: Keep-Alive
2. POST
A POST request is used to send data to the server, for example, customer information, file upload, etc. using HTML forms.(POST请求用于发送数据到服务器,例如,客户信息、文件上传等,使用HTML表单的方式。)here is how a POST request look like
The following is an example POST request to get the status of a job in AWS.
POST / HTTP/1.1 请求类型
content-type:application/x-www-form-urlencoded;charset=utf-8
host: https://importexport.amazonaws.com
content-length:207
上面3行为 请求头里面包含请求的终端地址
Action=GetStatus&SignatureMethod=HmacSHA256&JobId=JOBID&SignatureVersion=2&Version=2010-06-03&Signature=%2FVfkltRBOoSUi1sWxRzN8rw%3D&Timestamp=2011-06-20T22%3A30%3A59.556Z 这是请求体,请求的参数就是在这里,多个参数使用&分割
The first line represents the type of http request.
Lines 2-4 contain the HTTP headers, including the endpoint of the request.
After the HTTP headers, the body of the request contains the list of parameters. Each parameter is separated by an ampersand (&). The Action parameter indicates the action to perform For a list of the actions, see Actions.
HEAD
Same as GET, but only transfer the status line and header section. The HEAD method is functionally like GET, except that the server replies with a response line and headers, but no entity-bod
PUT
The PUT method is used to request the server to store the included entity-body at a location specified by the given URL.
DELETE
The DELETE method is used to request the server to delete a file at a location specified by the given URL
CONNECT
Establish a tunnel to the server identified by a given URI.
OPTIONS
Describe the communication options for the target resource.
TRACE
Perform a message loop-back test along the path to the target resource.
If you want to learn more about HTTP and building websites, I suggest you join the Build Responsive Real World Websites with HTML5 and CSS3 by Jonas Schmedtmann on Udemy. It's one of the best courses to learn modern web design, HTML5, and CSS3 step-by-step from scratch. Design AND code a huge project.
Difference between GET and POST Method
Now we know what the GET, and POST method does and ow let's understand what the difference between them is: GET和POST的区别如下:1) GET is a safe method ( idempotent), where POST is a non-idempotent method. An HTTP method is said to be idempotent if it returns the same result every time. (如果HTTP方法每次请求返回的结果都是一样的,就称这种方法是具有 幂等性 )HTTP methods GET, PUT, DELETE, HEAD, and OPTIONS are the idempotent method, and we should implement our application to make sure these methods always return the same result. (GET是幂等性方法,POST不是)
HTTP method POST is a non-idempotent method, and we should use the post method when implementing something that changes with every request. For example, to access an HTML page or image, we should use GET because it will always return the same object, but if we have to save customer information to the database, we should use the POST method. (如果每次请求返回的结果都一致,那么 使用GET方法,如:获取一个HTML页面或者一张图片;但是如果是保存客户信息 到数据库则应该使用POST方法)
Idempotent methods are also known as safe methods, and we don’t care about the repetitive request from the client for security methods.
2) We can only send limited data with the GET method, and it’s sent in the header request URL, whereas we can send a large amount of data with POST because it’s part of the request body.(GET请求只能发送有限的数据,因为这些数据是装在请求头里面的,而请求头是有大小限制的;然后POST发可以发送大量数据,因为这些数据是装在请求体里的,而请求体是可以容纳大量数据的)
3) GET method is not secure because data is exposed in the URL, and we can easily bookmark it and send a similar request again, POST is safe because the information is sent in the request body, and we can’t bookmark it. By the way, this would not be enough if security is a concern because HTTP requests can be intercepted en-route. Better use HTTPS or SSL encryption to make HTTP communication secure.(GET方法不安全!因为数据在RUL中是暴露着的,人们可以轻易的标签然后再次发送一个相似的请求;但是POST方法是安全的!因为发送的数据是在请求体里面的,是隐藏着的,不能进行标签。尽管如此,POST方法也不是绝对的安全,因为请求也可能会被中途拦截。更好的方式是使用HTTPS或者SSL进行加密处理来确保通信安全)