Você está na página 1de 6

Introduction to HTTP Hypertext Transfer Protocol (HTTP) The Hypertext Transfer Protocol (HTTP) is the protocol that programs

use to communicate over the World Wide Web. The main use for the protocol is for the interaction of web browsers and web servers, although there are many other applications !ommunication over "The Web# is facilitated by the use of re$uest and response messages. The browser (client process) sends a re$uest (for information) on behalf of a user and the web server replies with a response (hopefully containing the re$uested information ). HTTP dictates the style of these re$uest%response messages. The easiest way to get to grips with the protocol is to inspect some specific examples. Requests. &t the browser (client) we enter a '() to indicate the information we want, e.g. http://www.sumnaim.co.uk:80/ The browser interprets this '() as several fields, as follows* http:// 'se HTTP, the Hypertext Transfer Protocol to generate re$uest messages. www.sumnaim.co.uk !onnect to the named computer. We could use its +P address instead :80 !onnect to the remote computer by using port ,-. This is the default port number for web transactions so it is normally omitted. We can use any valid port number* . through /0010 (although some are in fact reserved for other use as "well23nown2ports#). / This is the document path on the remote computer. This is generally relative to the main document path on the server computer. +n this case % would be trying to gain access to this top2level path. The server will interpret this in whatever way it pleases often returning an HT4) index document. 5o we are more li3ely to use something li3e %index.html in our '() The browser will connect to the remote machine on port ,- by using soc3ets and a lower level protocol, probably T!P%+P. !lient%server programmers therefore need to have some 3nowledge of soc3et programming and the use of T!P%+P. 6n top of the T!P%+P communication we need to set up HTTP messages, so our browser might send a message something li3e*
GET / HTTP/1.1 Accept: image/gif, image/jpeg, te t/htm! Accept"#anguage: en"g$ Accept"Enco%ing: g&ip 'se("Agent: )o&i!!a/*.0 +compati$!e, )-.E /.01, 0in%ows 1T2 Host: www.sumnaim.co.uk 3onnection: 4eep"A!i5e

Page 1 of 6

Introduction to HTTP )et#s consider these 7 lines one at a time* )ine .. This is the Client Request line. +t indicates the function (method) that the client expects the server to use to respond to this message 8 in this case the GET method. +t also shows what the client wants to retrieve 8 in this case /, and finally the protocol to be used for the transaction (HTTP/ . ). 4ost servers will allow clients to use the older HTTP%..- version of the protocol too )ine 9. This is the first of / Header !ines. +t indicates what sort of documents that the client browser can cope with 8 in this case two image formats and html documents. )ine 1. 5hows that the preferred language is :nglish. This allows a client to specify one or more languages, in the event that the server has the same resource available in multiple languages. )ine ;. +ndicates that the browser can deal with compressed response bodies using g<ip. )ine 0. +dentifies the client as 4o<illa (version ;.-) running on Windows =T. +t also indicates that really the user is accessing 45 +nternet :xplorer v 0.-. ). )ine /. Tells the server what this browser thin3s the server#s hostname is. This is mandatory in HTTP%... )ine 7. Tells the server to 3eep the underlying T!P connection open until told to disconnect by the client. +n HTTP ... the default behaviour to 3eep the connection open (ma3e it persistent) until the client as3s for it to be closed. 'nder HTTP ..- the standard was to close the connection after the client re$uest was completed. )ine ,. The hidden line that we might forget 5imply a "lan# line. Responses. >iven a re$uest similar to that shown above, the server loo3s for a resource associated with "%# and returns it to the client. How the server interprets the resource varies, it may simply return a copy of a static file or it may dynamically generate the information The resource will be returned in a response message together with associated header fields. +n this case we might get a response message similar to*
HTTP/1.1 600 74 8ate: )on, 01 9an 600: 00:01:60 G)T -e(5e(: Apache/1.;.: +'ni 2 #ast")o%ifie%: -un, ;1 8ec 600/ 6;:*<:;0 G)T Etag: =;e>%e"16;"*/:$c>8a? Accept"@anges: $Ates 3ontent"#ength: 161 3onnection: c!ose 3ontent"tApe: te t/htm! Btit!eCG(eetingsDB/tit!eC Bh1CHappA 1ew Eea(DB/h1C 0e!come to 600: maA Aou enjoA goo% hea!th, happiness an% success.

Page 2 of 6

Introduction to HTTP The (esponse 4essage begins with a series of Header )ines giving information about document and the server itself. & blan3 line separates these Header )ines and the actual returned document (the body or entity or entity2body ). )et#s study this (esponse 4essage one line at a time* )ine .. 5hows the protocol used by the server (HTTP ...), followed by a $tatus Code and a Reason Phrase (9-- 6?). HTTP ... defines a range of 5tatus !odes and their corresponding (eason Phrases. )ine 9. +s the current time on the server using >reenwich 4ean Time (>4T). )ine 1. +ndicates the server application that is running i.e. &pache ..1./ on 'nix. )ine ;. 5pecifies the most recent modification time of the resource re$uested by the client. 6ften used for caching purposes at the client so the browser need not re$uest the entire resource again unless the modification time changes. )ine 0. This is an entity tag to provide the client with a uni$ue identifier for the server resource. )ine /. +ndicates that the server is capable of returning subsections (bytes) of a document, instead of supplying the whole document every time it is re$uested. This is useful when a client is accessing a database to retrieve records. )ine 7. >ives the number of bytes (characters) in the entity2body that follows the header lines. )ine ,. +ndicates that the server will close the connection on completion of its response. +f the client wants to send another re$uest it must open another connection. )ine @. Tells the browser what type of resource the server is supplying in the entity2body 8 in this case HT4) text. )ine .-. & blan3 line to separate the Header )ines from the entity2body. General HTTP %essa&e format. !lient (e$uest. )etho% '@# HTTP"5e(sion Gene(a!"hea%e( @eFuest"hea%e( EntitA"hea%e( EntitA"$o%A ('ote the "lan# line between the header fields and the :ntity2body )

Page 3 of 6

Introduction to HTTP The HTTP protocol specifies lots of headers 2 transactions seldom ma3e use of all of them. +n fact you could probably use Aust GET /in%e .htm! HTTP/1.1 without any header lines and still get a sensible response =ote that HTTP requests have the following general components* .. & method name, which tells the server what function (method) the client expects it to use to satisfy the re$uest. Possible HTTP ... methods are >:T, P65T, H:&B, P'T, )+=?, '=)+=?, B:):T:, 6PT+6=5 and T(&!:. The '() specifies the location of the resource to apply the method to. :ach server will interpret this in its own way but must respond using a standard HTTP ... response message. The last entry on the first line must specify the HTTP version used by the client. 9. 6ptional >eneral message Headers indicating such things as the current time. 1. (e$uest Headers to give the server more information about the client, e.g. its identity and the type of resource it can cope with. ;. :ntity headers to give information about an entity sent to a server, such as encoding schemes, length data type and origin (most re$uest messages do not have an :ntity2 body). 5erver (esponse. HTTP"5e(sion -tatus"co%e @eason"ph(ase Gene(a!"hea%e( @esponse"hea%e( EntitA"hea%e( EntitA"$o%A ('ote the "lan# line between the header fields and the :ntity2body ) +n the server response the general headers and entity headers are used in the same way as for a re$uest. The server will ma3e every effort to conform to the most compatible version of HTTP that the client is using. The status code indicates the result of the re$uest and the reason2 phrase is the human readable version of this. The response headers give the client information about the configuration of the server. +t informs the client what methods are supported, re$uests authori<ation or tells the client to try again later

Page 4 of 6

Introduction to HTTP $ome &eneral notes on methods and their data. >:T. When a browser sends a >:T message it generally has an empty entity2body because it is literally re$uesting the return of some resource. The name of that resource is supplied as the '(). +f the client needs to send some data as well this is placed at the end of the '() by appending a $uestion mar3, e.g. http*%%hostname%service%getBataCpage1, or to see if item .91; is in stoc3 we might send http*%%www.fredscompany.com%stoc3CitemD.91;. The client often caches the returned resource assuming that the user may want to access it again $uite soon P65T. +f the browser has re$uested a form and the user has completed it then the results may be P65Ted bac3 to the server in the entity2body. The browser probably will not cache the resource +t does not expect the user to re2use it in the near future Eragments. HT4) pages can be divided into subsections or fragments. Fy using the G symbol a browser can re$uest a fragment. +n actual fact it will probably re$uest the whole document and then display from the re$uired fragment onwards, e.g. if we wanted to loo3 at the +B: section of a page displaying dis3s we might set up the '() http*%%Aoesgroup.com%dis3sG+B:. The browser would probably Aust >:T the dis3s page and then display from +B: onwards. Huery 5trings. +f a user fills out a form or perhaps needs to interrogate a database at the server end, then the browser sends a Huery 5tring. This is appended to the '() by means of a $uestion mar3, e.g. suppose we want to $uery a clothes store to determine if it has any blue )evi Aeans with waist 19 inches and long leg length, the '() the browser produces might be*
http://www.c!othes.com/o(%e(GitemH#e5iI60jeansJco!ou(H$!ueJwaistH;6J!egH!ong

=otice that the $uery string Aoins all the fields by means of the ampersand (I). =ote also that, since the space character is a reserved symbol, we replace the space with a percentage sign (J) followed by the hex value (9-) for the &5!++ code for space (the space character has been replaced by an :5!ape code). The client may send the re$uest using >:T as its method and an empty body, e.g.
GET /o(%e(GitemH#e5iI60jeansJco!ou(H$!ueJwaistH;6J!egH!ong HTTP/1.1 K K K Host: www.c!othes.com K K K

6r, more li3ely, by using P65T as its method with the $uery string in its body, e.g.
P7-T /o(%e( HTTP/1.1 K K K Host: www.c!othes.com K K K GitemH#e5iI60jeansJco!ou(H$!ueJwaistH;6J!egH!ong

+t is up to the server to cope with this as best it can

Page 5 of 6

Introduction to HTTP

Parameters. The name%value pairs (e.g. waistD19) we have seen to give extra information to a server method are called parameters. The parameters can be supplied to a resource by separating them from the main '() by means of semi2colons, e.g. to indicate that we want to loo3 at e2type Aaguars we might use* http://www.ca(s.uk/jags,tApeHe +f several parameters are needed, a semi2colon separated list is formed. (hy different techniques) Bifferent protocol schemes use different '() styles. =ot only does HTTP use '()s but so does ETP and 54TP. The '()s vary slightly in their style but there is significant overlap. They all adhere to a general syntax that can be divided into nine (@) general parts*
BschemeC://Buse(C:Bpassw%CLBhostC:Bpo(tC/BpathC,Bpa(amsCGBFue(ACMBf(agC

+t is up to the client and server processes to ma3e their best attempt at a suitable interpretation of the scheme Re*ision of the &eneral form of messa&es. (emember CR/!+ means !arriage (eturn followed by )ine Eeed (in ! denoted by KrKn). (e$uest 4essage (client to server)
)etho% Space '@# Space HTTP"5e(sionCR/LF Gene(a!"hea%eCR/LF @eFuest"hea%e(CR/LF EntitA"hea%e(CR/LF CR/LF EntitA"$o%A

(esponse 4essage (server to client)


HTTP"5e(sion Space -tatus"co%e Gene(a!"hea%e(CR/LF @eFuest"hea%e(CR/LF EntitA"hea%e(CR/LF CR/LF EntitA"$o%A
Space

@eason"ph(aseCR/LF

References. Bavid >ourley and Frian Totty, HTTP The Definitive Guide* 6#(eilly 9--9. !linton Wong, HTTP Pocket Reference* 6#(eilly 9---.

Page 6 of 6

Você também pode gostar