[Switch] HTTP Request (difference between get and post) and response

Source: Internet
Author: User
Tags html header

HTTP Request (difference between get and post) and response

HTTP consists of two parts: request and response.

1. http request 1. http request format:

<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 [called the body].

2. Difference Between get and post

HTTP defines different methods for interaction with the server. The most basic methods are get and post (Ajax development only cares about get and post requests ).

The get and post methods have the following differences:

(1) on the client side, the get method submits data through the URL, and the data can be seen in the URL. In the post mode, the data is placed in the HTML header for submission.

(2) data submitted in get mode can contain a maximum of 1024 bytes, whereas post mode does not.

(3) security issues. As mentioned in (1), when get is used, the parameter is displayed in the address bar, but post is not. Therefore, if the data is Chinese and non-sensitive, use get. If the data you enter is not Chinese characters and contains sensitive data, use post as well.

(4) Safe and idempotent. The so-called security means that this operation is used to obtain information instead of modifying information. Idempotence means that multiple requests to the same URL should return the same result. The complete definition is not as strict as it looks. In other words, get requests generally do not have side effects. Basically, the goal is that when a user opens a link, she can be confident that the resource has not changed from her own perspective. For example, 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 safe and idempotent because it always returns the current news. And vice versa. POST requests are not that easy. Post indicates a request that may change resources on the server. Taking the news site as an example, the reader's comments to the article should be implemented through the POST request, because the site is already different after the annotation is submitted (for example, an annotation appears below the article ).

Example of get and post methods:

GET/books /? Name = Professional % 20 Ajax HTTP/1.1
HOST: www.wrox.com
User-Agent: Mozilla/5.0 (windows; U; Windows NT 5.1; en-US; RV: 1.7.6)
Gecko/20050225 Firefox/1.0.1
Connection: keep-alive

Post, HTTP, 1.1
HOST: www.wrox.com
User-Agent: Mozilla/5.0 (windows; U; Windows NT 5.1; en-US; RV: 1.7.6)
Gecko/20050225 Firefox/1.0.1
Content-Type: Application/X-WWW-form-urlencoded
Content-Length: 40
Connection: keep-alive
(---- Null Line here ----)
Name = Professional % 20 Ajax & publisher = Wiley

3. Differences between get and post methods in Form submission are summarized as follows:

(1) get is to get data from the server, and post is to send data to the server.

(2) For the form submission method, the server can only use request. querystring to obtain the data submitted in get mode, and the data submitted in post mode can only be obtained using request. form.

(3) In general, do not use the get method to submit a form because it may cause security problems. For example, if you use the get method in the login form, the user name and password entered by the user will be exposed in the address bar. However, in the paging program, the get method is better than the POST method.

Ii. Http response
1. Http response format:

<Status line>

<Headers>

<Blank line>

[<Response-body>]

The only real difference in the response is that the request information is replaced by the State Information in the first line. Status line describes the requested resources by providing a status code.

HTTP Response instance:

HTTP/1.1 200 OK
Date: sat, 31 Dec 2005 23:59:59 GMT
Content-Type: text/html; charset = ISO-8859-1
Content-Length: 122
<HTML>
<Head>
<Title> wrox homepage </title>
</Head>
<Body>
<! -- Body goes here -->
</Body>
</Html>
2. The most common status codes are:

◆ 200 (OK): The resource is found and everything is normal.
◆ 304 (not modified): The resource has not been modified since the last request. This is usually used for browser caching.
◆ 401 (unauthorized): the client has no permission to access this resource. This usually requires the user to enter the user name and password in the browser to log on to the server.
◆ 403 (Forbidden): the client is not authorized. This is usually because an incorrect user name or password is entered after 401.
◆ 404 (not found): the requested resource does not exist at the specified position.

Http get, post, and soap protocols all run on HTTP.
1) Get: The request parameter is appended to the URL as a sequence (query string) of a key/value pair.
The length of the query string is limited by the web browser and web server (for example, ie supports a maximum of 2048 characters), and is not suitable for transmitting large datasets.
At the same time, it is not safe
2) post: Request Parameters are transmitted in a different part of the HTTP header (Named Entity body). This part is used to transmit form information, so you must
Set Content-Type to application/X-WWW-form-urlencoded.
Post is designed to support user fields on web forms. Its parameters are also transmitted as key/value pairs.
However, it does not support complex data types, because post does not define the semantics and rules of the transmitted data structure.
3) soap: a special version of http post, which follows a special XML Message format.
Set Content-Type to text/XML
Any data can be XML-based






Differences between http post and get

1. Http only supports post and get command modes;

2. Post is designed to store up, while get is designed to fetch from the server. Get can also transmit less data to the server, the reason why get can also transmit data is to design and tell the server what data you actually need. post information is used as the content of the HTTP request, while get is transmitted in the HTTP header;

3. The post and get methods are different in HTTP transmission. Get parameters are transmitted in the HTTP header, while post data is transmitted in the HTTP request content;

4. When post is used to transmit data, it does not need to be displayed in the URL, but the get method must be shown in the URL;

5. Because the get method is limited by the URL length, it can only transmit about 1024 bytes. The size of post data transmission can reach 2 MB. According to Microsoft, Microsoft uses request. form () can receive up to 80 KB of data in IIS 4 and 100 kb in IIS 5;

6. Soap is implemented in the http post mode;

Example:

HTTP GET

Send

GET/demowebservices2.8/service. asmx/cancelorder? Userid = string & Pwd = string & orderconfirmation = string HTTP/1.1
HOST: api.efxnow.com

Reply

HTTP/1.1 200 OK
Content-Type: text/XML; charset = UTF-8
Content-Length: Length

<? XML version = "1.0" encoding = "UTF-8"?>
<Objplaceorderresponse xmlns = "https://api.efxnow.com/webservices2.3">
<Success> Boolean </success>
<Errordescription> string </errordescription>
<Errornumber> int </errornumber>
<Customerorderreference> long </customerorderreference>
<Orderconfirmation> string </orderconfirmation>
<Customerdealref> string </customerdealref>
</Objplaceorderresponse>

HTTP POST

Send

Post/demowebservices2.8/service. asmx/cancelorder HTTP/1.1
HOST: api.efxnow.com
Content-Type: Application/X-WWW-form-urlencoded
Content-Length: Length

Userid = string & Pwd = string & orderconfirmation = string

Reply

HTTP/1.1 200 OK
Content-Type: text/XML; charset = UTF-8
Content-Length: Length

<? XML version = "1.0" encoding = "UTF-8"?>
<Objplaceorderresponse xmlns = "https://api.efxnow.com/webservices2.3">
<Success> Boolean </success>
<Errordescription> string </errordescription>
<Errornumber> int </errornumber>
<Customerorderreference> long </customerorderreference>
<Orderconfirmation> string </orderconfirmation>
<Customerdealref> string </customerdealref>
</Objplaceorderresponse>

HTTP 1.2

Send

Post/demowebservices2.8/service. asmx HTTP/1.1
HOST: api.efxnow.com
Content-Type: Application/soap + XML; charset = UTF-8
Content-Length: Length

<? XML version = "1.0" encoding = "UTF-8"?>
<Soap12: envelope xmlns: xsi = "http://www.w3.org/2001/XMLSchema-instance" xmlns: XSD = "http://www.w3.org/2001/XMLSchema" xmlns: soap12 = "http://www.w3.org/2003/05/soap-envelope">
<Soap12: Body>
<Cancelorder xmlns = "https://api.efxnow.com/webservices2.3">
<Userid> string </userid>
<PWD> string </pwd>
<Orderconfirmation> string </orderconfirmation>
</Cancelorder>
</Soap12: Body>
</Soap12: envelope>

Reply

HTTP/1.1 200 OK
Content-Type: Application/soap + XML; charset = UTF-8
Content-Length: Length

<? XML version = "1.0" encoding = "UTF-8"?>
<Soap12: envelope xmlns: xsi = "http://www.w3.org/2001/XMLSchema-instance" xmlns: XSD = "http://www.w3.org/2001/XMLSchema" xmlns: soap12 = "http://www.w3.org/2003/05/soap-envelope">
<Soap12: Body>
<Cancelorderresponse xmlns = "https://api.efxnow.com/webservices2.3">
<Cancelorderresult>
<Success> Boolean </success>
<Errordescription> string </errordescription>
<Errornumber> int </errornumber>
<Customerorderreference> long </customerorderreference>
<Orderconfirmation> string </orderconfirmation>
<Customerdealref> string </customerdealref>
</Cancelorderresult>
</Cancelorderresponse>
</Soap12: Body>
</Soap12: envelope>

Related Article

Cloud Intelligence Leading the Digital Future

Alibaba Cloud ACtivate Online Conference, Nov. 20th & 21st, 2019 (UTC+08)

Register Now >

11.11 Big Sale for Cloud

Get Unbeatable Offers with up to 90% Off,Oct.24-Nov.13 (UTC+8)

Get It Now >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

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.