title: Java HTTP/HTTPS Proxy Server
body: <p>I'm writting my own Java Proxy Server. When I try to use it, I have the correct output (I see all commands that are going through the proxy server), but any website I want to visit doesn't work at all and I have the following error in my browser: ERR_TUNNEL_CONNECTION_FAILED. I don't actually good at network questions and there are some things I don't understand. So, there is my code below and I'll be really glad if someone tell me where is the problem:</p>
  
  <pre><code>import java.io.*;
  import java.net.*;
  
  public class ProxyServer
  {
      private String host;
      private int localPort;
      private int remotePort;
      private ProxyServer(String host, int localPort, int remotePort)
      {
          this.host=host;
          this.localPort=localPort;
          this.remotePort=remotePort;
      }
      public static void main(String[] args) throws IOException
      {
          new ProxyServer("localhost", 8080, 9001).start();
      }
      public void start() throws IOException
      {
          System.out.println("Starting the proxy server for "+this.host+":"+this.localPort+"...");
          ServerSocket ss=new ServerSocket(this.localPort);
          final byte[] request=new byte[4096];
          byte[] response=new byte[4096];
          while(true)
          {
              Socket client=null;
              Socket server=null;
              try
              {
                  client=ss.accept();
                  final InputStream streamFromClient=client.getInputStream();
                  OutputStream streamToClient=client.getOutputStream();
                  System.out.println(new BufferedReader(new InputStreamReader(streamFromClient)).readLine());
                  try
                  {
                      server=new Socket(host, this.remotePort);
                  }
                  catch(IOException exc)
                  {
                      PrintWriter out=new PrintWriter(streamToClient);
                      out.println("Proxy server cannot connect to "+host+":"+this.remotePort+"\n"+exc);
                      out.flush();
                      client.close();
                      continue;
                  }
                  InputStream streamFromServer=server.getInputStream();
                  final OutputStream streamToServer=server.getOutputStream();
                  System.out.println(new BufferedReader(new InputStreamReader(streamFromServer)).readLine());
                  new Thread
                  (
                      ()-&gt;
                      {
                          int bytesRead;
                          try
                          {
                              while((bytesRead=streamFromClient.read(request))!=-1)
                              {
                                  streamToServer.write(request, 0, bytesRead);
                                  streamToServer.flush();
                              }
                          }
                          catch(IOException exc){}
                          try
                          {
                              streamToServer.close();
                          }
                          catch(IOException exc){}
                      }
                  ).start();
                  int bytesRead;
                  try
                  {
                      while((bytesRead=streamFromServer.read(response))!=-1)
                      {
                          streamToClient.write(response, 0, bytesRead);
                          streamToClient.flush();
                      }
                  }
                  catch(IOException exc){}
                  streamToClient.close();
              }
              catch(IOException exc)
              {
                  System.out.println(exc.getMessage());
              }
              finally
              {
                  try
                  {
                      if(server!=null) server.close();
                      if(client!=null) client.close();
                  }
                  catch(IOException exc){}
              }
          }
      }
  }
  </code></pre>