Many web site instruct browsers to set cookies, small pieces of information that the browser will send back to the web site when it connects again. Many cookies are quite harmless and merely tell the server which page requests are related. However, some cookies are used for marketing and tracking purposes.

Your task is to write a program that prints all cookies that a web page wants to set. The cookies are a part of the HTTP header that is sent before the actual HTML document. Cookies are sent in lines that start with Set-Cookie:. However, letter case is not significant, and you need to be able to handle variations in letter case. Some web site even use a mixture of Set-Cookie:, set-cookie:, and so on, in the same page.

Your program should use sockets, not the URLConnection class.

Complete the following file:

CookiePrinter.java

import java.io.InputStream; import java.io.IOException; import java.io.OutputStream; import java.io.PrintWriter; import java.net.Socket; import java.util.Scanner; /** This program prints all cookies that a web site tries to set. Supply the name of the host and the resource on the command-line, for example java CookiePrinter nytimes.com / */ public class CookiePrinter { public static void main(String[] args) throws IOException { // Get command-line arguments if (args.length != 2) { System.out.println("Usage: java CookiePrinter host resource"); System.exit(0); } String host = args[0]; String resource = args[1]; // Open socket // Get streams // Turn streams into scanners and writers // Send command String command = "GET " + resource + " HTTP/1.0\n\n"; out.print(command); out.flush(); // Read server response and print any lines starting with // Set-Cookie, set-cookie, // Always close the socket at the end } }