A cookie is a string containing a key/value pair representing information of interest to the server such as user preferences. It is sent from a server to a browser. The browser should save the cookie to a file so that it can be used later.
A cookie is a string that consists of a name followed by an equal sign and then a value. The following is one possible cookie:
userID=Cookie Monster
A cookie can have multiple values. These values will be separated by a semicolon and white space.
We will use the HTTPServer
class and the HttpURLConnection
classes to demonstrate the handling of cookies. In the MyHTTPServer
class server's handler class's handle
method, add the following code after the other headers:
responseHeaders.set("Set-cookie", "userID=Cookie Monster");
When the server responds, it will send that cookie.
In the HttpURLConnectionExample
class's getResponse
method, add the following code at the beginning of its try block. A string is built containing the cookie text. Multiple...