Class SimpleHttpExchange

java.lang.Object
com.sun.net.httpserver.HttpExchange
dev.katsute.simplehttpserver.SimpleHttpExchange
All Implemented Interfaces:
Request, AutoCloseable

public abstract class SimpleHttpExchange extends HttpExchange
A HttpExchange with additional extensions to simplify usage.

Requests

GET Request

If a user sends a GET request to the server, a map of keys and values of that request can be retrieved by using the getGetMap() method.

POST Request

If a user sends a POST request to the server, a map of keys and values; or a MultipartFormData can be retrieved by using getPostMap() and getMultipartFormData().

multipart/form-data

For requests that have content type multipart/form-data, data must be retrieved using getMultipartFormData(), which returns a MultipartFormData using Records and FileRecords. Files sent through here are sent as a Byte array and not a File.

Cookies

A clients browser cookies for the site can be retrieved by using the getCookie(String) or getCookies() method.
Cookies can be set by using the setCookie(HttpCookie) or setCookie(String, String).
An exchange must be sent in order to change on the client.

Session

Normally the only "identifier" that we can retrieve from the user is their address and port provided from an exchange. This however, doesn't work across multiple tabs or when the user refreshes the page; instead we use a session cookie to track a user.
A server must have a HttpSessionHandler set using HttpServerExtensions.setSessionHandler(HttpSessionHandler) for this to work.
The HttpSessionHandler assigns session IDs to clients and allows the server to retrieve them using HttpServerExtensions.getSession(HttpExchange).

Response

To send response headers you must first retrieve then with HttpExchange.getResponseHeaders(), modify them, then send them using HttpExchange.sendResponseHeaders(int, long) or any other of the send methods.
Data can be sent as a Byte array, String, or as a File. Responses can optionally gziped to compress the data sent. Note: An exchange must be sent or closed, otherwise the connection may resend the request until it gets response or times out.
Since:
5.0.0
Version:
5.0.0
Author:
Katsute
See Also: