HTTP request method A total of 9 kinds, have options, HEAD, GET, Post and so on (the message header has a picture, very clear)

Source: Internet
Author: User
Tags xml dom document ftp protocol

Request Method: Specifies what the client wants to do with the specified resource/server
Here we describe the request methods available in http/1.1:


"Get: Get Resources"
The Get method is used to request resources that have been identified by the URI. The specified resource returns the response content after the server-side resolution (that is, if the requested resource is text, it is returned as it is, and if it is a cgi[Universal Gateway Interface), the executed output is returned.
Most commonly used to query the server for certain information. If necessary, you can append the query string parameter to the end of the URL to send the information to the server.
An error that often occurs when you use a GET request is that there is a problem with the format of the query string. The name and value of each parameter in the query string must be encoded with encodeurlcomponent () before it can be placed at the end of the URL, and all name-value pairs must be separated by (&), as in the following example:
Xhr.open ("Get", "01.php?name=foodoir&age=21", true);
The following function can be used to add query string parameters to the end of an existing URL:

     function Addurlparam (url,name,value) {        URL + = (Url.indexof ("?") = =-1? "?": "&");        URL + = encodeuricomponent (name) + "=" + encodeuricomponent (value);        return URL;    }

This Addurlparam function accepts three parameters: the URL to add the parameter to, the name of the parameter, and the value of the parameter.
The following is an example of using this function to construct a URL

    var url = "example.php";    Add parameter    url = addurlparam (URL, "name", "Foodoir");    url = addurlparam (URL, "age", "+");    Initialize request    Xhr.open ("Get", url,false);


"POST: Transfer entity text"
The Post method is used to transfer the body of the entity.
Although the body of the entity can also be transferred with the Get method, it is generally not transmitted using the Get method, but the Post method is used, although the Get method is similar to the Post method, but the main purpose of post is not to get the main content of the response.
The body of a POST request can contain very much data and is not limited in format. Here's an example:
Xhr.open ("Post", "01.php", true);
The second step in sending a POST request is to pass some data to the Send method, since XHR was originally designed to handle XML, so the XML DOM document can also be processed here, and the incoming document is serialized and submitted to the server as the request principal.
By default, the server does not treat post requests and submit Web forms as a request, so let's take a look at the following code:

function () {    var xhr = createxhr ();    Xhr.onreadystatechange = function () {        if (xhr.readystate = = 4) {//Detect XHR ReadyState Property            if (Xhr.status >= 200 && xhr.status <= 300) | | Xhr.status = = 304) {                alert (xhr.responsetext);            } else{                alert ("Request was unsuccessful:" + Xhr.status);}}    ;        Xhr.open ("Post", "post.php", true);    Xhr.setrequestheader ("Content-type", "application/x-www-form-urlencoded");    var form = document.getElementById ("ID");    Xhr.send (serialize (form));}

We can imitate XHR form submission: First set the Content-type header information to application/x-www-form-urlencoded, which is the type of form submission, The second is to create a string in the appropriate format (the Post data format is the same as the format of the query string), if you need to serialize the data of the form in the page and then send it to the server via the XHR function, you can use the serialization function serialize (), (form serialization, No specific introduction here)

Here we compare the difference between the Get method and the Post method in nature:

1, get method for information acquisition, it is safe (security: Refers to non-modified information, such as database information), and the Post method is used to modify the server resources on the request;

2. The data of GET request is appended to the URL, and the data submitted by the Post method is placed in the body of the HTTP message entity, so the Post method is more secure than the Get method.

3, the Get method transmits the amount of data is generally limited to 2KB, the reason is that: get is to submit data through a URL, and the URL itself is not limited to the data, but different browsers for the URL is limited, such as Internet Explorer for the URL limit of 2KB, and Chrome, The Firefox browser theoretically has no limit on the URL, its real limit depends on the operating system itself; The Post method is unlimited for data size, and the ability to really affect the size of the data is the server handler.

"HEAD: Get message Header"
The head method, like the Get method, does not return the body part of the leopard print to confirm the validity of the URI and the date and time of the resource update.
Specifically: 1, the judgment type; 2. Check the status code in the response to see if the object exists (response: The request executed successfully, but no data returned); 3. Test resources have been modified
The difference between the head method and the Get method: The Get method has entities, and the Head method has no entities.

"PUT: Transfer Files"
The Put method is used to transfer files, just like FTP protocol file uploads, requiring that the contents of the file be included in the body of the request message, and then saved in the location specified by the request URI. However, http/1.1 put method itself without authentication mechanism, anyone can upload files, there are security issues, it is generally not used.

"Delete: Delete Files"
Indicates that the client wants the server to delete a resource, as opposed to the Put method, which deletes the specified resource by URI

"options: Ask for supported methods"
The options method is used to query the method that specifies the resource support for the request URI (the client asks which request methods the server can submit)

"Trace: Trace Path"
The client can trace the transmission path of the request message by allowing the Web server to communicate the previous request back to the client's method

"Connect: Require tunneling protocol to connect proxies"
The Connect method requires a tunnel to be established when communicating with a proxy server to enable TCP communication with the tunneling protocol. The main use of SSL (Secure Sockets Layer) and TLS (Transport Layer Security) protocol to encrypt the communication after the network tunnel transmission.

More resources:

The 21st chapter of the JavaScript Advanced Programming (third edition);

The second chapter of graphic HTTP;

Http://www.cnblogs.com/foodoir/p/5911099.html

After the client connects to the server, it issues a message to the server that gets a WEB resource, which is called the client sends an HTTP request to the server. A complete HTTP request includes the following:
① Request Line
② several message headers (Request headers)
③ entity content (request body) may not have

    • Request method

There are 7 types of requests defined in HTTP: POST,, OPTIONS, DELETE, TRACE, PUT. The most common of these are GET and POST

1,get Request

[1] literally, a GET request is a request sent to the server for information.
[2] No special settings, by default, the browser is sent by the GET request, a total of 5, click the hyperlink, form submission is not set method, form submission settings method for Get mode, in the browser address bar directly enter the address access, AJAX set the request mode is GET.
[3] A GET request can also send request parameters to the server, in the form of a URL appended with a, request parameter name and value with = connection, multiple request parameters used & delimited. For example: Get/mail/1.html?name=abc&password=xyz http/1.1.

It is important to note that the data that can be carried by the GET method is limited, the data size is usually not more than 4K, not enough to submit a large number of form data, so in the form of the method of submission preferred POST.

2,post Request

[1] The literal meaning of the POST request is to send data to the server, only when the method= "POST" is set in the form, the request is post, and in the Ajax application, you can specify the request as post.
[2] The POST request places the request parameter in the request body, not after the URL address, and the size of the sending data is unlimited.
For get and POST2 requests, remember a word: get is typically used to fetch data from the server, and post is typically used to send data to the server.

    • Request message Header

A request message header, called a request header, is used to report some basic information about the browser side to the server.
Common Request Headers
Code description
accept:text/html,image/* types of data I support
ACCEPT-CHARSET:UTF-8 encoded character set for supported data
Accept-encoding:gzip supported compression methods
Languages supported by ACCEPT-LANGUAGE:EN-US,ZH-CN
host:localhost:8888 host name of the request
referer:http://www.baidu.com/index.jsp the URL to send the requested interface corresponding to the anti-theft chain
user-agent:mozilla/4.0 information about the browser
Connection:keep-alive when the request is complete, is it disconnected or connected?
Date:tue, 18:23:51 GMT send request time
cookie:tt=123 markup for a specific client

    • Request Body

GET request without request body, POST request: If the form form is submitted in the same way as POST, the data for the form item is sent to the server in the form of the request body, with no size limit.

http://blog.csdn.net/u011794238/article/details/46408051

HTTP request method A total of 9 kinds, have options, HEAD, GET, Post and so on (the message header has a picture, very clear)

Related Article

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.