120 likes | 212 Vues
Learn about HTTP and Java communication protocols, how messages are broken into packets, and the process of establishing connections between clients and servers using sockets. HTTP request and response types, methods, and handling through Java code are also covered.
E N D
Background • TCP/IP • Break message into packets • Give each packet a number and total number of packets in message • Destination IP address and port numbers • Source IP address and port • IP Address (v4 and v6) • Port Number (0 to 216-1) • Well Know (0-1023) • Registered (1024-49151) • Dynamic, private, ephemeral (all the rest)
Sockets • Internet Sockets • Listener or Server Socket • Client Sockets • Two way connection provides two way communication • Operating specific means of • Opening • Closing • Transmitting to and from a “socket” • Often uses a protocol like TCP/IP • In Java it is viewed as a stream
Hypertext Transfer Protocol • Application level • Provides communication between applications • Usually a client and server (though it may be peer-to-peer) • Underlying transport layer is TCP/IP • Request-Response Protocol • Used for sending and receiving various types of information • Used in web-applications especially in HTML-based applications
High-level View • An HTTP Session • Client establishes a TCP connection to a particular port (e.g. port 80) • Server waits for message from Client • Upon receipt the server sends back a • Status (e.g. ok) • Response • Requested Information • Error Message • Or it could be empty
Message Types:HTTP Request • Request Line • GET or POST with pathToResource • Header Lines • Header Name – Value Pairs • Example Names: From:, User-Agent: • Blank Line • Optional Message Body • Gets don’t have a Message Body • For example: XML Encoded information in a POST
Message TypesHTTP Response • Status Line • For example: • HTTP/1.0 200 OK • HTTP/1.0 404 Not Found • Header Lines • Header Name – Value Pairs • Example Names: Server:, Last-Modified: • Blank Line • Optional Message Body • Examples • HTML • XML Encoded Information is a response expected • Could be blank for a pure “POST”
Request Methods • GET • Should be used to retrieve information, not modify information on the server • Not enforced and some gets don’t conform to this suggestion, can cause problems • Encodes parameters in URL sent to server • Appears in the request line (the first line) • Server may have URL length limits (e.g. 2083 characters)
Request Methods • POST • Should be used to change information • Information sent in message body • Can be any format (including binary) • We will use XML • Is of arbitrary size
HTTP And Java • Several “HTTP” servers written in Java • Jetty • Apache • Oracle HTTPServer • Built into Oracle’s Java 1.6 and 1.7 • Import com.sun.net.httpserver.*; • We will use this one
HttpServer Code • HttpServer server; • HTTPServer.create( new InetSocketAddress(portNumber), maxNumberOfConnections); • server.setExecutor(null); • Default thread pool manager • server.createContext(URLSuffix, handlerMethodObject) • Handler for various suffixes in the URL • Handler “method” is a Command object that extends HttpHandler • HttpHandler has one method: void handle(HttpExchange exchange) • Often the handler is declared as an instance of an anonymous inner type that overrides the “void handle(HttpExchangeexchange)” method • server.start()
HttpExchange Code • An HttpExchangeencapsulates an HTTP request received and a response to be generated in one exchange. • exchange.sendResponseHeaders(intrCode, responseLength) • rCode is often a constant found in HttpURLConnection • Examples • HttpURLConnection.HTTP_INTERNAL_ERROR • HttpURLConnection.HTTP_OK, • responseLength • 0 means an arbitrary size • -1 means no response body • exchange.getRequestBody(); • Gets the body of the request as an input stream • Used as a parameter to xmlStream.fromXML(exchange.getRequestBody()); • exchange.getResponseBody(); • Gets the body of the request as an input stream • Used as a parameter to xmlStream.toXML(object, exchange.getResponseBody());