A basic HTTP session

Because HTTP is a client-server protocol, an HTTP session consists of three phases:
1. The client establishes a TCP connection (or the appropriate connection if the transport layer is not TCP).
2. The client sends its request and then waits for the answer.
3. The server processes the request and sends back its answer, containing a status code and the appropriate
Starting with HTTP/1.1, the connection is no longer closed after the third phase, as the client is allowed to issue
another request at this point: the second and third phases can therefore be done several times.

Establishing a connection
Because HTTP is a client-server protocol, it always is the client that establishes the connection. Opening a
connection in HTTP really is establishing a connection in the underlying transport layer, usually TCP.
With TCP, the default port for an HTTP server on a computer is port 80, though others are often used, like 8000 or
8080. The URL of a page to fetch contains both the domain name and the port number, though the latter can be
omitted if it is 80.

Note: The client-server model does not allow the server to send data to the client without an explicit request for it. To work
around this problem, web developers use several techniques: pinging the server periodically via the XMLHTTPRequest
Javascript object, using the HTML WebSockets API, or similar protocols.

Sending a client request
Once the connection is established, the user-agent can send its request. (A user-agent is typically a web browser,
but need not be.) A client request consists of text directives, separated by CRLF (carriage return, followed by line
feed), divided in three blocks:
1. The first line contains a request method followed by its parameters:
the path of the document, i.e., an absolute URL without the protocol and the domain name
the HTTP protocol version used
2. The subsequent lines each represent a specific HTTP header, giving the server some information about
what kind of data is appropriate (e.g., what language, what MIME types) or some data altering its behavior
(e.g., not sending an answer if it is already cached). These HTTP headers form a block that ends with an

and finally sends a response back.1  3 Content‐Length: 64  4 Content‐Type: application/x‐www‐form‐urlencoded  5 6   name=Joe%20User&request=Send%20me%20one%20of%20your%20catalogue Host: developer. and telling the server that the user-agent would prefer the page in French.g.  http://developer.empty line. 3. data size. hints about caching). Sending the result of a form: 1 2 POST /contact_form. the status line.. which contains the data (if any).mozilla. As there is no Content‐Length: HTTP header. Similarly to the block of HTTP headers for a client request. 3. a server response is formed of text directives.org. the data block is empty and the server can process the request as soon as it receives the empty line marking the end of the headers.mozilla. The final block is the data block. these HTTP headers form a block that ends with an empty line. i. Example requests Fetching the root page of developer. 2. which contains further data and is mainly used by the POST method. The first line.org/.e.mozilla. Similarly to a client request. consists of an acknowledgment of the HTTP version used followed by a status request (and its meaning in human-readable text). though divided in three different blocks: 1. separating the data block from the headers block. compression algorithm used.mozilla. The subsequent lines each represent a specific HTTP header giving the client some information about the data sent (e. separated by CRLF. if possible: 1 2 3 GET / HTTP/1. the web server handles it. type.org Accept‐Language: fr Note the final empty line. The final block is the optional data block.php HTTP/1. Example responses Successful reception of a web page .org Structure of a server response After the connected agent has sent its request.1  Host: developer.

 09 Oct 2010 14:33:02 GMT  Server: Apache  Last‐Modified: Tue.3 (Red Hat) Server at developer. (here comes the 29769 bytes of the requested web page) Notification that the requested resource has been permanently moved 1 HTTP/1.org/">here</a>.org Port 80</address>  </body></html> Notification that the requested resource doesn't exist 1 2 3 4 HTTP/1.1 2 3 HTTP/1.2. 01 May 2007 14:24:39 GMT  . max=98  7 8 Accept‐Ranges: bytes  Via: Moz‐Cache‐zlb05  9 Connection: Keep‐Alive  10 X‐Cache‐Info: caching  11 X‐Cache‐Info: caching  Content‐Length: 325 (the content contains a default page to display if the user‐agent is not a 12 13   14 <!DOCTYPE HTML PUBLIC "‐//IETF//DTD HTML 2.2. 09 Oct 2010 14:28:02 GMT  Server: Apache  4 Last‐Modified: Tue.. it is expected 6 Keep‐Alive: timeout=15.mozilla. charset=iso‐8859‐1  4 Date: Sat.1 404 Not Found  Date: Sat.1 301 Moved Permanently  2 3 Server: Apache/2.3 (Red Hat)  Content‐Type: text/html.mozilla.1 200 OK  Date: Sat.mozilla. 01 Dec 2009 20:18:22 GMT  5 ETag: "51142bc1‐7449‐479b075b2891b"  6 7 Accept‐Ranges: bytes  Content‐Length: 29769  8 Content‐Type: text/html  9   10 <!DOCTYPE html.org/ (this is the new link to the resource..0//EN">  15 16 17 18 19 20 21 22 <html><head>  <title>301 Moved Permanently</title>  </head><body>  <h1>Moved Permanently</h1>  <p>The document has moved <a href="https://developer. 09 Oct 2010 14:30:24 GMT  5 Location: https://developer.</p>  <hr>  <address>Apache/2.

5 ETag: "499fd34e‐29ec‐42f695ca96761. .1. CPU time is saved. Network congestion is diminished by lowering the total amount of TCP packets (fewer opening and closing TCP packets). They allow transmitting several requests on the same TCP connection (or on the specific connected transport layer if the HTTP is not built upon TCP/IP). (contains a site‐customized page helping the user to find the missing resour Persistent connections Persistent connections were introduced in HTTP/1.. HTTP is more adaptive: the cost for trying a feature is considerably lowered as an error response no longer leads to closing the connection. requests can be pipelined to save part of the connection latency. By opening and closing fewer TCP connections.. The TCP stack has more time to detect network congestion and to adapt its sending and receiving windows. This has several advantages: Because the connection can be reused.48fe7523cfcc1"  6 7 Accept‐Ranges: bytes  Content‐Length: 10732  8 9 10 Content‐Type: text/html    <!DOCTYPE html.