title: HTTP CONNECT Tunnel reply with BAD Request (400)
body: <p>I have this code for HTTP Tunnel</p>
  
  <pre><code>private static void doTunnelHandshake(Socket conn, String proxyHost, int proxyPort, String host, int port) throws IOException {
  
      String msg = "CONNECT " + host + ":" + port + " HTTP/1.1\n" + "Host: https://" + proxyHost + ":" + proxyPort + "\r\n" + "User-Agent: " + sun.net.www.protocol.http.HttpURLConnection.userAgent+ "\r\n\r\n";
  
  OutputStream out = conn.getOutputStream();
  
  byte b[];
  try {
      /*
       * We really do want ASCII7 -- the http protocol doesn't change with
       * locale.
       */
      b = msg.getBytes("ASCII7");
  } catch (UnsupportedEncodingException ignored) {
      /*
       * If ASCII7 isn't there, something serious is wrong, but Paranoia
       * Is Good (tm)
       */
      b = msg.getBytes();
  }
  
  out.write(b);
  out.flush();
  
  /*
   * We need to store the reply so we can create a detailed error message
   * to the user.
   */
  byte reply[] = new byte[200];
  int replyLen = 0;
  int newlinesSeen = 0;
  boolean headerDone = false; /* Done on first newline */
  
  InputStream in = conn.getInputStream();
  
  while (newlinesSeen &lt; 2) {
      int i = in.read();
      if (i &lt; 0) {
          throw new IOException("Unexpected EOF from proxy");
      }
      if (i == '\n') {
          headerDone = true;
          ++newlinesSeen;
      } else if (i != '\r') {
          newlinesSeen = 0;
          if (!headerDone &amp;&amp; replyLen &lt; reply.length) {
              reply[replyLen++] = (byte) i;
          }
      }
  }
  
  /*
   * Converting the byte array to a string is slightly wasteful in the
   * case where the connection was successful, but it's insignificant
   * compared to the network overhead.
   */
  String replyStr;
  try {
      replyStr = new String(reply, 0, replyLen, "ASCII7");
  } catch (UnsupportedEncodingException ignored) {
      replyStr = new String(reply, 0, replyLen);
  }
  
  
  
  /* We asked for HTTP/1.0, so we should get that back */
  if (!replyStr.startsWith("HTTP/1.0 200")) {
      throw new IOException("Unable to tunnel for " + host + ":" + port + ".  TunnelProxy returns \"" + replyStr + "\"");
  } 
  
  /* tunneling Handshake was successful! */
  }
  </code></pre>
  
  <p>but apache replies</p>
  
  <blockquote>
    <p>"HTTP/1.1 400 Bad Request"</p>
  </blockquote>