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 the URLConnection class, not sockets. Use the getHeaderFields method.

Complete the following file:

import; import; import; import; import java.util.List; import java.util.Map; /** 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 / */ 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 URLConnection // Get header fields Map<String, List<String>> headerFields = connection.getHeaderFields(); // print any lines starting with Set-Cookie, set-cookie, etc. for (. . .) { . . . System.out.println(key + ": " + cookie); } } }