title: Why am I getting a HTTP 400 bad request response when sending a manually-crafted HTTP request over a TCP socket?
body: <p>I'm trying to build an HTTP request manually (as a string) and send it over a TCP socket, this is what I'm trying to do: </p>
  
  <pre><code>import java.io.BufferedReader;
  import java.io.IOException;
  import java.io.InputStreamReader;
  import java.io.PrintWriter;
  import java.net.MalformedURLException;
  import java.net.Socket;
  import java.net.URL;
  import java.util.logging.Level;
  import java.util.logging.Logger;
  
  /**
   * SimpleHttpClient.java (UTF-8)
   *
   * Mar 27, 2014
   *
   * @author tarrsalah.org
   */
  public class SimpleHttpClient {
      private static final String StackOverflow = "http://stackoverflow.com/";
  
      public static void main(String[] args) {
  
  //      if (args.length &lt; 1) {
  //          System.out.println("Usage : SimpleHttpClient &lt;url&gt;");
  //          return;
  //      }
  
          try {
              URL url = new URL(StackOverflow);
              String host = url.getHost();
              String path = url.getPath();
              int port = url.getPort();
              if (port &lt; 80) {
                  port = 80;
              }
  
              //Construct and send the HTTP request
              String request = "GET" + path + "HTTP/1.1\n";
              request += "host: " + host;
              request += "\n\n";
  
              // Open a TCP connection
              Socket socket  = new Socket(host, port);
              // Send the request over the socket
              PrintWriter writer = new PrintWriter(socket.getOutputStream());
              writer.print(request);
              writer.flush();
              // Read the response
              BufferedReader reader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
              String next_record = null;
              while ((next_record = reader.readLine()) != null) {
                  System.out.println(next_record);
              }
              socket.close();
          } catch (MalformedURLException ex) {
              Logger.getLogger(SimpleHttpClient.class.getName()).log(Level.SEVERE, null, ex);
          } catch (IOException ex) {
              Logger.getLogger(SimpleHttpClient.class.getName()).log(Level.SEVERE, null, ex);
          }
      }
  }
  </code></pre>
  
  <p>But I'm getting this response message whatever the URL is chosen: </p>
  
  <pre><code>HTTP/1.0 400 Bad request
  Cache-Control: no-cache
  Connection: close
  Content-Type: text/html
  
  &lt;html&gt;&lt;body&gt;&lt;h1&gt;400 Bad request&lt;/h1&gt;
  Your browser sent an invalid request.
  &lt;/body&gt;&lt;/html&gt;
  </code></pre>
  
  <p>Why ?</p>