Results 1 to 4 of 4
  1. #1
    sakshi.13agg is offline Member
    Join Date
    Sep 2011
    Posts
    14
    Rep Power
    0

    Default Working with proxy

    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.

    Please help me out!

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is online now Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,247
    Rep Power
    19

    Default Re: Working with proxy

    Moved from Advanced Java

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  3. #3
    sakshi.13agg is offline Member
    Join Date
    Sep 2011
    Posts
    14
    Rep Power
    0

    Default Re: Working with proxy

    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...
    Last edited by sakshi.13agg; 10-25-2012 at 01:33 PM.

  4. #4
    DarrylBurke's Avatar
    DarrylBurke is online now Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,247
    Rep Power
    19

    Default Re: Working with proxy

    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

Similar Threads

  1. Webstart application through proxy not working
    By gonzjav in forum Advanced Java
    Replies: 1
    Last Post: 05-23-2012, 04:53 PM
  2. openConnection(Proxy proxy) question
    By Dark in forum New To Java
    Replies: 8
    Last Post: 12-31-2011, 01:15 PM
  3. Proxy.pac file not working in IE9
    By choag in forum New To Java
    Replies: 0
    Last Post: 08-26-2011, 03:42 PM
  4. Almost got my proxy server working
    By SegFault in forum Advanced Java
    Replies: 0
    Last Post: 09-22-2010, 05:32 PM
  5. Http - proxy or non-proxy ?
    By Shiv in forum Networking
    Replies: 0
    Last Post: 04-11-2009, 08:07 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •