title: Proxy server in Java: Not able to send a response back to the client
body: <p>I'm trying to write a piece of code to implement a proxy server. However, I am not able t send data back to the client as the response appears as follows.</p>
  
  <p>The Response shows a normal response and a few responses come up later which says URI is too large.</p>
  
  <pre><code>HTTP/1.1 414 Request-URI Too Large
  Date: Sun, 01 Sep 2013 14:52:20 GMT
  Server: Apache/2.2.22 (Ubuntu)
  Vary: Accept-Encoding
  Content-Length: 325
  Connection: close
  Content-Type: text/html; charset=iso-8859-1
  
  &lt;!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN"&gt;
  &lt;html&gt;&lt;head&gt;
  &lt;title&gt;414 Request-URI Too Large&lt;/title&gt;
  &lt;/head&gt;&lt;body&gt;
  &lt;h1&gt;Request-URI Too Large&lt;/h1&gt;
  &lt;p&gt;The requested URL's length exceeds the capacity
  limit for this server.&lt;br /&gt;
  &lt;/p&gt;
  &lt;hr&gt;
  &lt;address&gt;Apache/2.2.22 (Ubuntu) Server at 127.0.1.1 Port 80&lt;/address&gt;
  &lt;/body&gt;&lt;/html&gt;
  </code></pre>
  
  <p>The code is as follows.</p>
  
  <pre><code>package ownproxy;
  
  /**
   *
   * @author mklrjv
   */
  import java.net.*;
  import java.io.*;
  import java.nio.CharBuffer;
  import java.util.logging.Level;
  import java.util.logging.Logger;
  
  public class InterceptionProxy2 {
  
      public static void main(String[] args) throws IOException {
          ServerSocket serverSocket = null;
          boolean listening = true;
          int port = 1234;
          try {
              serverSocket = new ServerSocket(port);
          } catch (IOException e) {
              System.out.println("Port Error");
              System.exit(-1);
          }
          while (listening) {
              new ProxyThread2(serverSocket.accept()).start();
          }
          serverSocket.close();
      }
  }
  
  class ProxyThread2 extends Thread {
  
      private Socket socket = null;
      private static final int BUFFER_SIZE = 32768;
  
      public ProxyThread2(Socket socket) {
          super("ProxyThread2");
          this.socket = socket;
      }
  
      public void run() {
          PrintWriter outGoing = null;
          try {
              outGoing = new PrintWriter(socket.getOutputStream(), true);
              BufferedReader inComing = new BufferedReader(new InputStreamReader(socket.getInputStream()));
              String incomingRequest;
              String url = "";
              String request = "";
              String response = "";
              //Take the incoming request
              char[] buf = new char[1000000];
              inComing.read(buf);
              request = new String(buf);
              //Create a new socket for connecting to destination server
              Socket connSocket = new Socket("localhost", 80);
              PrintWriter pOut = new PrintWriter(connSocket.getOutputStream(), true);
              BufferedReader pIn = new BufferedReader(new InputStreamReader(connSocket.getInputStream()));
              //Put data into the new socket(to the apache server) and receive its output
              pOut.print(request);
              pIn.read(buf);
              response = new String(buf);
              System.out.println(response);
              //Put data back into the original client socket
              outGoing.write(response);
          } catch (IOException ex) {
              Logger.getLogger(ProxyThread2.class.getName()).log(Level.SEVERE, null, ex);
          } finally {
              outGoing.close();
          }
  
      }
  }
  </code></pre>