A Basic HTTP Session - HTTP - MDN
A Basic HTTP Session - HTTP - MDN
Establishing a connection
Because HTTP is a client-server protocol, it always is the client that establishes the connection. Opening a
connection inHTTP 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.
empty line.
3. The nal block is the optional data block, which contains further data and is mainly used by the POST
method.
Example requests
Fetching the root page of developer.mozilla.org, i.e. http://developer.mozilla.org/, and telling the server that the
user-agent would prefer the page in French, if possible:
1
2
3
GET/HTTP/1.1
Host:developer.mozilla.org
AcceptLanguage:fr
Note the nal empty line, separating the data block from the headers block. As there is no ContentLength:
HTTPheader, 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.
Sending the result of a form:
1
2
POST/contact_form.phpHTTP/1.1
ContentLength:64
ContentType:application/xwwwformurlencoded
5
6
name=Joe%20User&request=Send%20me%20one%20of%20your%20catalogue
Host:developer.mozilla.org
Example responses
Successful reception of a web page
1
2
3
HTTP/1.1200OK
Date:Sat,09Oct201014:28:02GMT
Server:Apache
LastModified:Tue,01Dec200920:18:22GMT
ETag:"51142bc17449479b075b2891b"
6
7
AcceptRanges:bytes
ContentLength:29769
ContentType:text/html
10
<!DOCTYPEhtml...(herecomesthe29769bytesoftherequestedwebpage)
HTTP/1.1301MovedPermanently
2
3
Server:Apache/2.2.3(RedHat)
ContentType:text/html;charset=iso88591
Date:Sat,09Oct201014:30:24GMT
Location:https://developer.mozilla.org/(thisisthenewlinktotheresource;itisexpected
KeepAlive:timeout=15,max=98
7
8
AcceptRanges:bytes
Via:MozCachezlb05
Connection:KeepAlive
10
XCacheInfo:caching
11
XCacheInfo:caching
ContentLength:325(thecontentcontainsadefaultpagetodisplayiftheuseragentisnota
12
13
14
<!DOCTYPEHTMLPUBLIC"//IETF//DTDHTML2.0//EN">
15
16
17
18
19
20
21
22
<html><head>
<title>301MovedPermanently</title>
</head><body>
<h1>MovedPermanently</h1>
<p>Thedocumenthasmoved<ahref="https://developer.mozilla.org/">here</a>.</p>
<hr>
<address>Apache/2.2.3(RedHat)Serveratdeveloper.mozilla.orgPort80</address>
</body></html>
1
2
3
4
HTTP/1.1404NotFound
Date:Sat,09Oct201014:33:02GMT
Server:Apache
LastModified:Tue,01May200714:24:39GMT
ETag:"499fd34e29ec42f695ca96761;48fe7523cfcc1"
6
7
AcceptRanges:bytes
ContentLength:10732
8
9
10
ContentType:text/html
<!DOCTYPEhtml...(containsasitecustomizedpagehelpingtheusertofindthemissingresour
Persistent connections
Persistent connections were introduced in HTTP/1.1. They allow transmitting several requests on the same TCP
connection (or on the specic connected transport layer if the HTTPis not built upon TCP/IP). This has several
advantages:
Because the connection can be reused, requests can be pipelined to save part of the connection latency.
By opening and closing fewer TCP connections, CPUtime is saved.
Network congestion is diminished by lowering the total amount of TCPpackets (fewer opening and closing
TCPpackets).
The TCPstack has more time to detect network congestion and to adapt its sending and receiving windows.
HTTPis more adaptive: the cost for trying a feature is considerably lowered as an error response no longer
leads to closing the connection.