1 / 12

HTTP and Java

HTTP and Java. 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 2 16 -1) Well Know (0-1023) Registered (1024-49151)

stormy
Télécharger la présentation

HTTP and Java

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. HTTP and Java

  2. 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)

  3. 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

  4. 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

  5. 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

  6. 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

  7. 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”

  8. 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)

  9. 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

  10. 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

  11. 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()

  12. 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());

More Related