title: Coding a CONNECT method in Java
body: <p>Im trying to code a HTTP proxy in Java. My proxy mandatorily communicates with another proxy. Just it, receives a GET request from a localhost port, and throw to the external proxy. Then, take the external proxy response and throw to the client.</p>
  
  <p>The GET method is working very well. But I dont know how to handler the CONNECT method. </p>
  
  <p>I receives the CONNECT request from the cliente, throw to the external proxy, and receives from him a 200 Connection Established status. That means the external proxy is ready to forward every thing to the requested HOST. I send that 200 status to the cliente too. But and now, what I do?</p>
  
  <p><a href="https://i.stack.imgur.com/jB5by.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/jB5by.jpg" alt="enter image description here"></a></p>
  
  <p>And here comes the code:</p>
  
  <pre><code>try {
      onLogReceived("&lt;.&gt; Thread inject: abrindo comunicação com " + this.hostDest + ":" + this.hostDestPort, LOG_LEVEL_INFO);
      socketHostDest = new Socket(this.hostDest, this.hostDestPort);
  
      try {
          inCliente = this.cliente.getInputStream();
  
          onLogReceived("&lt;_&gt; Thread inject: recebendo requisição cliente.", LOG_LEVEL_DEBUG);
          Requisicao reqCliente = getReqCliente(inCliente);
          reqCliente.setStrMacro(this.strMacro);
  
          outDestino = socketHostDest.getOutputStream();
  
          onLogReceived("&lt;_&gt; Thread inject: enviando requisição cliente.", LOG_LEVEL_DEBUG);
          writeStream(reqCliente.getStrRequisicao(), outDestino);
  
          inDestino = socketHostDest.getInputStream();
  
          onLogReceived("&lt;_&gt; Thread inject: recebendo resposta do destino.", LOG_LEVEL_DEBUG);
          String respostaDestino = getInputStreamStr(inDestino, TAM_BUFFER_RECEPCAO);
          String statusLine = respostaDestino.substring(0, respostaDestino.indexOf('\r'));
  
          onLogReceived("&lt;.&gt; Thread inject: Status line: " + statusLine, LOG_LEVEL_INFO);
  
          outCliente = this.cliente.getOutputStream();
  
          onLogReceived("&lt;_&gt; Thread inject: enviando resposta ao cliente.", LOG_LEVEL_DEBUG);
          writeStream(respostaDestino, outCliente);
  
          if (statusLine.contains(CONNECT_ESTABLISHED)) {
  
  //######################################################################
  //Its right here. I already receive the 200 code from the external proxy
  //and already sent it to the client. Now we have opened the streams:
  //
  //outCliente: OutputStream to the client
  //inCliente: InputStream to the client
  //outDestino: OutputStream to the external proxy
  //inCliente: OutputStream to the external proxy
  //######################################################################
  
          }
  </code></pre>