Results 1 to 4 of 4
Thread: Working with proxy
- 10-25-2012, 09:11 AM #1
Member
- Join Date
- Sep 2011
- Posts
- 14
- Rep Power
- 0
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!
- 10-25-2012, 10:43 AM #2
Re: Working with proxy
Moved from Advanced Java
dbIf you're forever cleaning cobwebs, it's time to get rid of the spiders.
- 10-25-2012, 12:04 PM #3
Member
- Join Date
- Sep 2011
- Posts
- 14
- Rep Power
- 0
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********************"); } }
Please tell me what is wrong...Last edited by sakshi.13agg; 10-25-2012 at 02:33 PM.
- 10-25-2012, 02:09 PM #4
Re: Working with proxy
If you're forever cleaning cobwebs, it's time to get rid of the spiders.
Similar Threads
-
Webstart application through proxy not working
By gonzjav in forum Advanced JavaReplies: 1Last Post: 05-23-2012, 05:53 PM -
openConnection(Proxy proxy) question
By Dark in forum New To JavaReplies: 8Last Post: 12-31-2011, 02:15 PM -
Proxy.pac file not working in IE9
By choag in forum New To JavaReplies: 0Last Post: 08-26-2011, 04:42 PM -
Almost got my proxy server working
By SegFault in forum Advanced JavaReplies: 0Last Post: 09-22-2010, 06:32 PM -
Http - proxy or non-proxy ?
By Shiv in forum NetworkingReplies: 0Last Post: 04-11-2009, 09:07 AM
Bookmarks