Вы находитесь на странице: 1из 26

KSK 8033 SOCKET PROGRAMMING

END OF LESSON, STUDENT SHOULD BE ABLE:-


1. To Describe What Is Http
2. Describe The Function Of Http
3. Describe The Request Body On Http
4. Describe The Http Method
5. Describe The Cookies

LEARNING OBJECTIVE
Communication web browser and web Html file, picture,window file .exe and
servers anything (represent in bytes)

HTTP

HYPERTEXT TRANSFER
PROTOCOL

How web client talk to How data is transfer


server from server client
ILLUSTRATED HTTP PROCESS
At application Layer that allow the
web application to communicate and
exchange data
HTTP WORKS
Sequence of http process
1. The client opens a TCP connection to the server on port 80, by
default; other ports may be specified in the URL.

2. The client sends a message to the server requesting the resource at


specified path. The request includes a header, and optionally
depending on the nature of the request) a blank line followed by data
for the request.

3. The server sends a response to the client. The response begins with
a response code, followed by a header full of metadata, a blank line,
and the requested document or an error message.

4. The server closes the connection.


CLIENT REQUEST
successful response )
HTTP/1.1 200 OK
Date: Sun, 21 Apr 2013 15:12:46 GMT
Server: Apache
Connection: close
Content-Type: text/html; charset=ISO-8859-1
Content-length: 115
<html>
<head>
<title>
A Sample HTML file
</title>
</head>
<body>
The rest of the document goes here
</body>
</html>
HTTP 1.1 RESPONSE CODE
MAIN METHOD
An
method
idempotent
is a
HTTP
HTTP IDEMPOTENT
method that can be called
many times without
different outcomes.
It would not matter if
the method is called only
once, or ten times over.
The result should be the
same.
EXAMPLE OF POST METHOD
REQUEST BODY send by client

RESPONSE BODY return by server


REQUEST BODY
GET /docs/index.html HTTP/1.1 Host: www.nowhere123.com
Accept: image/gif, image/jpeg, */*
Accept-Language: en-us Accept-Encoding: gzip, deflate
User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)
(blank line)

When this request message reaches the server, the server can take either one of
these actions:
1. The server interprets the request received, maps the request into a file under
the server's document directory, and returns the file requested to the client.
2. The server interprets the request received, maps the request into a program
kept in the server, executes the program, and returns the output of the
program to the client.
3. The request cannot be satisfied, the server returns an error message.
REQUEST BODY
POST /cgi-bin/register.pl HTTP 1.0
Date: Sun, 27 Apr 2013 12:32:36
Host: www.cafeaulait.org
Content-type: application/x-www-form-urlencoded
Content-length: 54
username=Elliotte+Harold&email=elharo%40ibiblio.org

1. A starter line including the method, path and query string, and HTTP version
2. An HTTP header
3. A blank line (two successive carriage return/linefeed pairs)
4. The body
RESPONSE BODY
HTTP/1.1 200 OK The browser receives the
response message,
Date: Sun, 18 Oct 2009 08:56:53 GMT
interprets the message and
Server: Apache/2.2.14 (Win32) displays the contents of the
Last-Modified: Sat, 20 Nov 2004 07:16:26 GMT message on the browser's
ETag: "10000000565a5-2c-3e94b66c2e680" window according to the
Accept-Ranges: bytes media type of the response
(as in the Content-Type
Content-Length: 44
response header). Common
Connection: close media type include
Content-Type: text/html "text/plain", "text/html",
X-Pad: avoid browser bug "image/gif", "image/jpeg",
<html><body><h1>It works!</h1></body></html> "audio/mpeg",
"video/mpeg",
"application/msword", and
"application/pdf".
EXERCISE
1. HTTP is stand for ______________________________________
A. Hypertext protocol
B. Hypertext markup protocol
C. Hypertext Technology Protocol
D. Hypertext Transfer Protocol

2. Which method used for request body ?


A. POST
B. GET
C. PUT
D. All the answer above

Short question:
1. What is mean by idempotent?
2. Give at least 2 difference between http 1.0 and http 1.1?
COOKIES
Example of COOKIES
An HTTP cookie (web cookie, browser cookie)
is a small piece of data that a server sends to
the user's web browser

session IDs, shopping cart contents, login


credentials, user preferences, and more
HTTP HEADER SET COOKIES CART
HTTP/1.1 200 OK
Content-type: text/html
Set-Cookie: cart=ATVPDKIKX0DER

the server doesnt reuse cookies, this enables it to track individual users
and sessions across multiple,

request sets a user cookie for the entire foo.example.com domain:


Set-Cookie: user=elharo;Domain=.foo.example.com
COOKIES MANAGER
Java 5 includes an abstract
java.net.CookieHandler class that defines an API
for storing and retrieving cookies.
Declaration of cookies class and object
CookieManager manager = new CookieManager();
CookieHandler.setDefault(manager);

After installing a CookieManager with those two lines of code, Java will
store any cookies sent by HTTP servers you connect to with the URL
class, and will send the stored cookies back to those same servers in
subsequent requests

Вам также может понравиться