HTTPHTTP messagesWhat the client does, part IOther methodsWhat the client does, part IIClient request headersWhat the client does, part IIIWhat the server does, part ICommon status codesWhat the server does, part IIViewing the responseThe GetResponses program, IThe GetResponses program, IIServer response headersWhat the server does, part IIIThe <meta http-equiv> tagSummaryThe EndJan 14, 2019HTTPHypertext Transfer Protocol2HTTP messagesHTTP is the language that web clients and web servers use to talk to each otherHTTP is largely “under the hood,” but a basic understanding can be helpfulEach message, whether a request or a response, has three parts:1. The request or the response line2. A header section3. The body of the message3What the client does, part IThe client sends a message to the server at a particular port (80 is the default)The first part of the message is the request line, containing:A method (HTTP command) such as GET or POSTA document address, andAn HTTP version numberExample:GET /index.html HTTP/1.04Other methodsOther methods beside GET and POST are:HEAD: Like GET, but ask that only a header be returnedPUT: Request to store the entity-body at the URIDELETE: Request removal of data at the URILINK: Request header information be associated with a document on the serverUNLINK: Request to undo a LINK requestOPTIONS: Request information about communications options on the serverTRACE: Request that the entity-body be returned as received (used for debugging)5What the client does, part IIThe second part of a request is optional header information, such as:What the client software isWhat formats it can acceptAll information is in the form Name: ValueExample: User-Agent: Mozilla/2.02Gold (WinNT; I)Accept: image/gif, image/jpeg, */*A blank line ends the header6Client request headersAccept: type/subtype, type/subtype, ...Specifies media types that the client prefers to acceptAccept-Language: en, fr, dePreferred language (For example: English, French, German) User-Agent: stringThe browser or other client program sending the requestFrom: [email protected]Email address of user of client programCookie: name=valueInformation about a cookie for that URLMultiple cookies can be separated by commas7What the client does, part IIIThe third part of a request (after the blank line) is the entity-body, which contains optional dataThe entity-body part is used mostly by POST requestsThe entity-body part is always empty for a GET request8What the server does, part IThe server response is also in three partsThe first part is the status line, which tells:The HTTP versionA status codeA short description of what the status code meansExample: HTTP/1.1 404 Not FoundStatus codes are in groups: 100-199 Informational 200-299 The request was successful 300-399 The request was redirected 400-499 The request failed 500-599 A server error occurred9Common status codes200 OKEverything worked, here’s the data301 Moved PermanentlyURI was moved, but here’s the new address for your records302 Moved temporarilyURL temporarily out of service, keep the old one but use this one for now400 Bad RequestThere is a syntax error in your request403 ForbiddenYou can’t do this, and we won’t tell you why404 Not FoundNo such document408 Request Time-out, 504 Gateway Time-outRequest took too long to fulfill for some reason10What the server does, part IIThe second part of the response is header information, ended by a blank lineExample:Content-Length: 2532Connection: CloseServer: GWS/2.0Date: Sun, 01 Dec 2002 21:24:50 GMTContent-Type: text/htmlCache-control: privateSet-Cookie: PREF=ID=05302a93093ec661:TM=1038777890:LM=1038777890:S=yNWNjraftUz299RH; expires=Sun, 17-Jan-2038 19:14:07 GMT; path=/; domain=.google.comAll on one line11Viewing the responseThere is a header viewer at http://www.delorie.com/web/headers.html(with nasty jittery advertisements)Example 2.3 (GetResponses) in the Gittleman book does the same thingHere’s an example (from GetResponses):% java GetResponses http://www.cis.upenn.edu/~matuszek/cit597- 2003/index.htmlStatus line: HTTP/1.1 200 OKResponse headers: Date: Wed, 10 Sep 2003 00:26:53 GMT Server: Apache/1.3.26 (Unix) PHP/4.2.2 mod_perl/1.27 mod_ssl/2.8.10 OpenSSL/0.9.6e Last-Modified: Tue, 09 Sep 2003 19:24:50 GMT ETag: "1c1ad5-1654-3f5e2902” Accept-Ranges: bytes Content-Length: 5716 Keep-Alive: timeout=15, max=100 Connection: Keep-Alive Content-Type: text/html12The GetResponses program, IHere’s just the skeleton of the program that provided the output on the last slide:import java.net.*;import java.io.*;public class GetResponses { public static void main(String [ ] args) { try { ...interesting code goes here... } catch(Exception e) { e.printStackTrace(); } }}13The GetResponses program, IIHere’s the interesting part of the code:URL url = new URL(args[0]);URLConnection c = url.openConnection();System.out.println("Status line: ");System.out.println('\t' + c.getHeaderField(0));System.out.println("Response headers:");String value = "";int n = 1; while (true){ value = c.getHeaderField(n); if (value == null) break; System.out.println('\t' + c.getHeaderFieldKey(n++) + ": " + value);}14Server response headersServer: NCSA/1.3Name and version of the serverContent-Type: type/subtypeShould be of a type and subtype specified by the client’s Accept headerSet-Cookie: name=value; optionsRequests the client to store a cookie with the given name and value15What the server does, part IIIThe third part of a server response is the entity bodyThis is often an HTML pageBut it can also be a jpeg, a gif, plain text, etc.--anything the browser (or other client) is prepared to accept16The <meta http-equiv> tagThe <meta http-equiv=string content=string> tag may occur in the <head> of an HTML documenthttp-equiv and content typically have the same kinds of values as in the HTTP headerThis tag asks the client to pretend that the information actually occurred in the headerThe information is not really in the headerThis tag is available because you have little direct control over what is in the header (unless you write your own server)As usual, not all browsers
View Full Document