0

How can i redirect all the browser request to a local ServerSocket before submiting to internet using java. I was just trying to create a local proxy server. Please help ...

Currenty NOT WORKING code

BufferedReader reader = null;
try
{
    ServerSocket ss = new ServerSocket(8080);
    while(true)
    {
        Socket s = ss.accept();
        reader = new BufferedReader(new InputStreamReader(s.getInputStream(), "UTF-8"));
        String line;
        while ((line = reader.readLine()) != null)
        {
            System.out.println(line);
        }
    }
}
catch (IOException ex)
{
    System.err.println(ex);
}
finally
{
    try
    {
        reader.close();
    }
    catch (IOException e)
    {
        e.printStackTrace();
    }
}
Antoniossss
  • 31,590
  • 6
  • 57
  • 99

1 Answers1

0

http://www.java2s.com/Code/Java/Network-Protocol/Asimpleproxyserver.htm

That is the example of proxy server. You will have to configure your browser to use it. Besides, using google doesn't hurt

Edit: Your code works fine with firefox. This is what I have received when trying to get google page.

Accepted connection from /127.0.0.1:49233
GET http://google.pl/ HTTP/1.1
Host: google.pl
User-Agent: Mozilla/5.0 (Windows NT 6.2; WOW64; rv:30.0) Gecko/20100101 Firefox/30.0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: pl,en-US;q=0.7,en;q=0.3
Accept-Encoding: gzip, deflate
Cookie: PREF=ID=3726591df27f540a:U=ab8413c778d20cca:FF=0:LD=pl:NW=1:TM=1401430486:LM=1403779198:SG=2:S=KJ5wGcrbznlQF3a6; NID=67=kqWLLShO-xj4HPZRs7k-xDtASHYmnY3oTSkC6ndHVJBBNKOqhrmnynUrxiRh4mX3t4w7KaaOLtJInflI9MXjxCqrTDQgXcTLlvvbOMWb9ovQJOFz090oXRrkqXhK5ftTpDnTT1H8Z7tJRMwiOecMKcE4wu4u0KM; OGPC=4061130-2:5-5:
Connection: keep-alive
Antoniossss
  • 31,590
  • 6
  • 57
  • 99
  • I had configured IE proxy with localhost and port 8080. Still request is not coming to local ServerSocket started. – user3778611 Jun 26 '14 at 09:58
  • Who in the world use IE these days? I don't have it even try verify your issue. http://www.w3schools.com/browsers/browsers_stats.asp - wow whole 8% – Antoniossss Jun 26 '14 at 10:00
  • I have tried with Crome but request didn't reach the ServerSocket opened. – user3778611 Jun 26 '14 at 10:05
  • And webpage did open normally? If yes, that you set it wrong, if no eg. timeout, than you should show us some code sir. – Antoniossss Jun 26 '14 at 10:14
  • This is the only code that i added for listening. I think the rest need to be done in browser. – user3778611 Jun 26 '14 at 10:30
  • On the Connections tab of the Internet Options -> On the Local Area Network Settings, cleared the Automatically detect settings check box ->Under Proxy server, selected the "Use a proxy server for your LAN (These settings will not apply to dial-up or VPN connections)" check box -> Specifed the address to localhost and port to 8080, and then unselect the Bypass proxy server for local addresses check box. – user3778611 Jun 26 '14 at 10:34
  • Code should be in question. Ill do it for you – Antoniossss Jun 26 '14 at 10:39
  • Its working for you! Okay thanks Antonios ... i am not able to get this in my machine even if i give proxy to local Server may be some application running in my machine to connect directly to internet. i am not sure. Thanks Antonios for spending time on this. – user3778611 Jun 26 '14 at 11:06
  • maybe you should try 127.0.0.1 instead of localhost – Antoniossss Jun 26 '14 at 11:28
  • Told you it is something wroing with your configuration:) – Antoniossss Jun 27 '14 at 06:01