Excerpt from: http://www.cnblogs.com/artech/p/restful-web-api-01.html
One, TCP/IP and HTTP
TCP/IP is the general name of a complete set of network protocols that are based on IPs and TCP protocols, so we sometimes call them TCP/IP protocol clusters. It is no exaggeration to say that TCP/IP underpins the entire Internet because it is the Internet protocol used by the Web. The TCP/IP protocol cluster is divided into 4 levels as shown in the right figure [2] (Application layer, Transport layer, network layer and link layer), and the sub-protocols constituting the whole protocol cluster are at the corresponding level.
Now that the entire protocol cluster is named TCP/IP, the two protocols are naturally the most important of all. IP datagram transmissions provided by the IP protocol at the network layer are unreliable because it only promises to send datagrams as much as possible, but there is no guarantee that the datagrams sent will be able to reach the destination successfully. The unreliability of the IP protocol is also reflected in its inability to detect whether the data has changed during transmission, which means that the integrity of the data is not guaranteed. IP protocol is a non-connected (connectionless) network protocol, the processing of each datagram is independent of it, so the IP protocol also does not provide for the orderly transmission (the order of data received and sent in the same order) guarantee.
Although the IP protocol can only provide unreliable data transmission, but also the orderly transmission is not guaranteed, but the Transport layer protocol based on it to effectively solve the two problems. TCP is a connection-based protocol in which the two parties need to establish a connection before the message is transmitted, and the connection needs to be closed after the message is transmitted. This is a duplex (Duplex) connection, and the duplex of the data interchange can use it to send data to each other.
TCP uses the "Receive acknowledgement" and "time-out retransmission" mechanisms to ensure that the data arrives at the destination successfully. Specifically, the receiving party replies to a confirmation message after the data is successfully received. The sender locally has a buffer that holds the sent message that has not been confirmed, and if the sender does not receive a confirmation message for a given text within a set time limit, it will select the corresponding message from that buffer to resend it. After the acknowledgement is received, the corresponding message is removed from the buffer.
In order to solve the problem of orderly transmission, the sender will number each message, the sequence of the message reflects the order in which they are sent. After receiving a message, the receiver uses this sequence number to determine if it has a transmitted text that has not been successfully received, and if so, the message is stored in a local buffer. When all the previously sent messages are received, the receiving party submits the received messages sequentially up (the application layer), and the successfully submitted messages are removed from the buffer. In addition to receiving "out of order" messages, the receiver may also receive duplicate messages, because no message has a unique sequence number, if the sequence number is less than successfully submitted or added to the buffer sequence of the message, it will be considered as a duplicate received message and discarded.
Since each TCP message segment has a 16-bit inspection and (Checksum), the receiver can confirm that the data has been tampered with during transmission. In addition, TCP also provides a "flow control" function to avoid the loss of packets due to inconsistent buffer size. Specifically, if the sender's buffer is larger than the receiver's cache, it will cause the receiver to be unable to process the subsequent received packets in the case of a buffer full, so the receiver will notify the sender of the remaining size of their buffers in a timely manner, the latter to control the message to send "traffic".
HTTP (hypertext Transfer Protocol), all called Hypertext Transfer Protocol, is part of the TCP/IP protocol cluster. As can be seen from figure 1-1, this is a network protocol located in the application layer, under which is the TCP protocol. Because the TCP protocol is a "reliable" protocol, HTTP can naturally provide reliable data transfer capabilities.
The IP protocol uses the IP address to locate the destination sent by the datagram, and the domain Name System (DNS) enables the conversion between the domain name and the IP address. The TCP protocol uses the port number to identify the application, so an application must specify the IP address (or domain name) and port number of the target application when communicating using the TCP protocol. HTTP defaults to a port number of 80, while HTTPS (using TLS/SSL to provide transport security for HTTP) has a default port number of 443, of course, if the network is available, we can specify any port.
Response Status Code
For any HTTP request sent by the client to the Web server, regardless of the circumstances, each response has a status code and corresponding descriptive text represented by a 3-digit number. The status codes of different values embody different types of response states, and the range of response status codes is regulated by the following.
- 100~199: Information status code, on behalf of the request has been accepted, need to continue processing.
- 200~299: Success Status Code, which indicates that the request has been successfully received, understood, and accepted by the server.
- 300~399: Redirect status code, representing the need for the client to take further action to complete the request.
- 400~499: The client Error status code, which indicates that the client appears to have an error that prevents the server from processing.
- 500~599: Server error status code, which means that the server in the process of processing the request error or abnormal state occurs, it is also possible that the server is aware that the current hardware and software resources can not complete the processing of the request.
Iv. HTTP Messages
The message that the client and Web server Exchange in a single HTTP transaction is called the HTTP header, and the request message that the client sends to the server is called the request packet, and the response message that the server returns to the client is called the response header. The request message and the response header are encoded in plain text, consisting of a simple string of lines. A complete HTTP message consists of the following three components.
- Start line: Represents the first line of text for an HTTP message, the request message takes advantage of the starting line to represent the HTTP method used, the request URI, and the HTTP version adopted, while the starting line of the response message is loaded with information such as HTTP version and response status code.
- Header collection: The starting line of an HTTP message can contain 0 or more header fields after it. Each header behaves as a key/value pair, with the key and value representing the header name and the value of the header, separated by a colon (":"). The HTTP quote is marked with a blank line as the end of the header collection.
- Main content: The empty line that represents the end flag of the header collection is the main part of the HTTP message. The data that the client submits to the server is generally placed in the principal of the request header, and the body of the response header also hosts the data returned to the client by the server. Either the request message or the response message, the main part of it can be the default.
Next we look at a specific HTTP newspaper stationery what kind of structure. The following text snippet reflects the HTTP request that we access to Microsoft's official website (www.microsoft com) via Chrome, which represents the three basic properties of the HTTP request, the HTTP method (GET), the target resource ( www.microsoft.com/en-us/default.aspx) and protocol version (http/1.1).
1:get http://www.microsoft.com/en-us/default.aspx http/1.1
2:host:www.microsoft.com
3:connection:keep-alive
4:cache-control:max-age=0
5:user-agent:mozilla/5.0 (Windows NT 6.1) applewebkit/535.7 (khtml, like Gecko) chrome/16.0.912.75 safari/535.7
6:accept:text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
7:accept-encoding:gzip,deflate,sdch
8:accept-language:en-us,en;q=0.8
9:accept-charset:iso-8859-1,utf-8;q=0.7,*;q=0.3
10:cookie: ...
The above request message does not have a principal, so everything outside the start line is a collection of headers, and we can get the host name, the cache policy adopted, the browser-related information, the media type supported by the client, the encoding method, the language, the character set, and so on.
The preceding HTTP request is sent to the server via the browser and receives a response message with the following structure, which allows us to get the HTTP version (http/1.1) and the Response status code ("OK") from its starting line, which indicates that the request was received for normal processing. The content of the response is encapsulated in the body part of the response message, and its media type is represented by the header "Content-type". Because the main content of the response message is an HTML document, the "Content-type" header represents a media type of "text/html".
1:http/1.1 OK
2:cache-control:no-cache
3:pragma:no-cache
4:content-type:text/html; Charset=utf-8
5:content-encoding:gzip
6:expires:-1
7:vary:accept-encoding
8:server:microsoft-iis/7.5
9:x-aspnet-version:2.0.50727
10:vtag:791897542300000000
11:p3p:cp= "All IND DSPs COR ADM CONo CUR CUSo ivao ivdo PSA PSD TAI TELo our SAMo CNT COM INT NAV ONL PHY PRE PUR UNI"
12:x-powered-by:asp.net
13:date:wed, Jan 07:06:25 GMT
14:content-length:34237
: <! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">
:
[1] Hypertext/hypermedia (Hypertext/hypermedia): Hypertext is an electronic document that renders textual content, at the core of which is the ability to access the referenced document directly using the inline hyperlink (Hyperlink). Hypermedia is a simple extension of hypertext to render multimedia content (compared to slices, audio, and video). HTML documents are the most typical hypertext/hypermedia files that we often have.
[2] In addition to the use of this 4-tier division method, there are two other typical division. One of these is the addition of a physical layer based on the physical network hardware below the link layer, and this partitioning method has no essential difference. The other is to divide the TCP/IP protocol cluster into 7 levels including application layer, presentation layer, session layer, Transport layer, network layer, link layer and physical layer.
Web standards-related