package edu.pdx.cs399J.web;

import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.List;
import java.util.Map;

/**
 * Demonstrates making an HTTP GET request using the {@link java.net.URLConnection} class
 *
 * @author David Whitlock
 * @since Summer 2008
 */
public class UrlHttpGet {

  /**
   * Fetches the content of a URL using an HTTP GET
   *
   * @param urlString The URL to GET
   * @throws java.io.IOException If a problem occurs while reading URL
   */
  private static void getURL(String urlString) throws IOException {
    URL url = new URL(urlString);
    HttpURLConnection conn = (HttpURLConnection) url.openConnection();
    conn.setRequestMethod("GET");
    conn.setRequestProperty("Hello", "World");

    conn.setDoOutput(true);
    PrintWriter pw = new PrintWriter(conn.getOutputStream(), true);
    pw.print("Hello=World");
    pw.flush();

    conn.connect();

    PrintStream out = System.out;
    out.println("Headers");

    Map<String,List<String>> headers = conn.getHeaderFields();
    for (String key : headers.keySet() ) {
      out.print("  " + key + ": ");
      for (String value : headers.get(key)) {
        out.print(value);
      }
      out.println("\n");
    }

    out.println("\nContent");
    out.flush();

    BufferedReader br = new BufferedReader(new InputStreamReader(conn.getInputStream()));
    do {
      out.println(br.readLine());
    } while (br.ready());

    br.close();
  }

  public static void main(String[] args) throws IOException {
    String url = null;

    for (String arg : args) {
      if (url == null) {
        url = arg;

      } else {
        usage("Extraneous command line argument: " + arg);
      }
    }

    if (url == null) {
      usage("Missing URL");
    }

    getURL(url);
  }

  private static void usage(String message) {
    PrintStream err = System.err;
    err.println("** " + message);
    err.println("usage: java UrlHttpGet url");
    System.exit(1);
  }
}

@@ -706,198 +706,8 @@
 %22);%0A
-    conn.setRequestProperty(%22Hello%22, %22World%22);%0A%0A    conn.setDoOutput(true);%0A    PrintWriter pw = new PrintWriter(conn.getOutputStream(), true);%0A    pw.print(%22Hello=World%22);%0A    pw.flush();%0A%0A
     
@@ -718,24 +718,24 @@
 .connect();%0A
+
 %0A    PrintSt
@@ -912,19 +912,62 @@
 int(%22  %22
- + 
+);%0A      if (key != null) %7B%0A        out.print(
 key + %22:
@@ -967,24 +967,32 @@
 ey + %22: %22);%0A
+      %7D%0A
       for (S

Handle a null header key.