This discussion is archived
0 Replies Latest reply: Oct 25, 2012 4:48 AM by 970632 RSS

Problem in  creating a forward proxy

970632 Newbie
Currently Being Moderated
I want to create a forward proxy .
And the requirement is that whenever browser hits a URL then that request should first go to that proxy, I want to add a header to this request then the modified request should go back to browser through which it will reach the remote server.

I tried to implement this through running a port on which I will read the request coming from browser .But I do not know how will I send the modified request back to the browser.
I have written the following code:

Java Code:
import java.io.*;
import java.net.*;
 
 
public class ProxyApp {
 
    public static void main(String a[]) throws IOException , Exception
    {
        ServerSocket proxySocket = new ServerSocket(7456);
        System.out.println("Socket created at port number.....7456.");
         
 
         
        System.out.println("waiting for browser to connect......");
         
        Socket browserSocket    = proxySocket.accept();
         
        System.out.println("request received from browser......");
         
         
        OutputStream browserOutputStream    = browserSocket.getOutputStream();
        InputStream browserInputStream      = browserSocket.getInputStream();
         
        byte[]       b = new byte[1];
 
        StringBuffer buf = new StringBuffer();
        String       s ;
 
        for ( ; ; ) {
            int len ;
 
            len = browserInputStream.read(b, 0, 1);
            if ( len == -1 ) {
                break ;
            }
            s = new String( b );
            buf.append( s );
            if ( b[0] != '\n' ) {
                continue ;
            }
            break ;
        }
 
        String bufferedData = buf.toString();
        System.out.println("browser requested......"+bufferedData);
         
 
        int  start, end ;
         
        start = bufferedData.indexOf( ' ' ) + 1;
        while ( bufferedData.charAt(start) == ' ' ) {
            start++ ;
        }
        end   = bufferedData.indexOf( ' ', start );
        String urlString = bufferedData.substring( start, end );
 
        System.out.println("browser requested..urlString...."+urlString);
 
         
         /*******************reading from server********************************/
        URL url = new URL(urlString);
        URLConnection connection = url.openConnection();
        connection.setDoOutput(true);
 
        OutputStream remoteOutputStream =  connection.getOutputStream();
         
        System.out.println("sent to remote......"+bufferedData);
         
        remoteOutputStream.write(bufferedData.getBytes());
        remoteOutputStream.flush();
        remoteOutputStream.close();
         
        System.out.println("Data written to remote server...");
         
        
        InputStream remoteInputStream   = connection.getInputStream();
 
        byte[]       b1 = new byte[1];
 
        StringBuffer buf1 = new StringBuffer();
        String       s1 ;
 
        for ( ; ; ) {
            int len ;
 
            len = remoteInputStream.read(b1, 0, 1);
            if ( len == -1 ) {
                break ;
            }
            s1 = new String( b1 );
            buf1.append( s1 );
            if ( b1[0] != '\n' ) {
                continue ;
            }
            break ;
        }
 
        String bufferedData1 = buf1.toString();
          
        System.out.println("Data read from remote server...bufferedData1..."+bufferedData1);
          
         
        browserOutputStream.write(bufferedData1.getBytes());
        browserOutputStream.flush();
        browserOutputStream.close();
          
        System.out.println("Data written to browser...");
         /**********************************************/
 
 
         
 
        System.out.println("**************END********************");
    }
}
But while getting the input stream from url connection I get Socket Exception.
Please tell me what is wrong...
Please help me out!

Edited by: 967629 on Oct 25, 2012 4:48 AM

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points