Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: Implementing web proxy

  1. #1
    Junior Member
    Join Date
    Feb 2013
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Implementing web proxy

    I am presently trying to implement a web proxy but i have run into a few issues.


    1.when my web proxy works for some sites ,it runs into a loop where the request and response gets called over and over again.I think it has something to do with my multiple thread clients
    2.For some reason,My webproxy does not work for most web sites.I can't figure out why this is the case



     
      import java.net.*;
            import java.io.*;
     
          public class ClientHandler implements Runnable{
    	    Socket socket;
    	    Socket serverSocket;
    	    private InputStream clientinput;
    	    private OutputStream clientoutput;
    	    private HttpRequest httprequest;
    	    private HttpResponse httpresponse;
    	    BufferedWriter toClient;
            BufferedWriter toServer;
    	    public ClientHandler(Socket socket){
    		this.socket=socket;
    		try {
    			this.clientinput=socket.getInputStream();
     
    			this.clientoutput=socket.getOutputStream();
    		} catch (IOException e) {
    			// TODO Auto-generated catch block
    			e.printStackTrace();
    		}
    		httprequest= new HttpRequest(clientinput);
    		   System.out.println("Accepted a request!\n" + httprequest);
     
    	}
     
    	  @Override
    	     public void run() {
    		 while (true){
    			  System.out.println("Accepted a request!\n" + httprequest);
    			  while(httprequest.busy);
    			    URL url = null;
                    try {
                        url = new URL(httprequest.getRequestURL());
                    } catch (MalformedURLException e){
                        try {
    						url = new URL("http:\\"+httprequest.getRequestHost()+httprequest.getRequestURL());
    					} catch (MalformedURLException e1) {
    						// TODO Auto-generated catch block
    						e1.printStackTrace();
    					}
                    }
                    {
                    int remotePort = (url.getPort() == -1) ? 80 : url.getPort();
                    System.out.println("I am going to try to connect to: " + url.getHost() + " at port " + remotePort);
                    try {
    					serverSocket = new Socket(url.getHost(), remotePort);
    					 System.out.println("Connected.");
    				} catch (UnknownHostException e) {
    					// TODO Auto-generated catch block
    					e.printStackTrace();
    				} catch (IOException e) {
    					// TODO Auto-generated catch block
    					e.printStackTrace();
    				}
     
     
     
                    //write to the server - keep it open.
                    System.out.println("Writing to the server's buffer...");
                    try {
    					toServer = new BufferedWriter(new OutputStreamWriter(serverSocket.getOutputStream()));
    				} catch (IOException e) {
    					// TODO Auto-generated catch block
    					e.printStackTrace();
    				}
                    try {
    					toServer.write(httprequest.getFullRequest());
    				} catch (IOException e) {
    					// TODO Auto-generated catch block
    					e.printStackTrace();
    				}
                    try {
    					toServer.flush();
    				} catch (IOException e) {
    					// TODO Auto-generated catch block
    					e.printStackTrace();
    				}
                    System.out.println("flushed.");
     
     
     
                    System.out.println("Getting a response...");
                    try {
    					httpresponse = new HttpResponse(serverSocket.getInputStream());
    				} catch (IOException e) {
    					// TODO Auto-generated catch block
    					e.printStackTrace();
    				}
                    System.out.println("Got a response!\n" + httpresponse);
                    while(httpresponse.isBusy());   
                           }
     
     
                    toClient = new BufferedWriter(new OutputStreamWriter(clientoutput));
                    try {
    					     toClient.write(httpresponse.getFullResponse());//seems there is a problem here
    					toClient.flush();
    		            toClient.close();
     
    				} catch (IOException e) {
    					// TODO Auto-generated catch block
    					e.printStackTrace();
    				}
     
     
     
     
    		   }//while
     
    	       }
     
                }
     
     
     
     
     
          import java.io.IOException;
          import java.net.InetSocketAddress;
          import java.net.ServerSocket;
          import java.net.Socket;
     
     
     
     
         public˚ class Server implements Runnable {
    	 ServerSocket proxyserver=null;
    	 InetSocketAddress adr;
    	 Socket socket;
    	 ClientHandler handler;
     
    	 public Server(){
     
    		adr= new InetSocketAddress("localhost",8088);
        	try {
    			proxyserver= new ServerSocket();
    			proxyserver.bind(adr);
    			System.out.println("Serversocket created");
    		} catch (IOException e) {
     
    			e.printStackTrace();
    		}
    	}
    	public static void main(String[] args) {
    		new Thread(new Server()).start();
    	}
     
    	@Override
    	public void run() {
    		try {
     
    			  new Thread( new ClientHandler(proxyserver.accept())).start();  // calls the start method to start a thread which also starts the run method
    		} catch (IOException e) {
    			// TODO Auto-generated catch block
    			e.printStackTrace();
    		}
     
     
    	}
    }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Implementing web proxy

    does not work for most web sites
    I think many sites can detect a robot program vs a user with a browser.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Feb 2013
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Implementing web proxy

    Quote Originally Posted by Norm View Post
    I think many sites can detect a robot program vs a user with a browser.
    Apart from that ,Is theer anything you find wrong in my code.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Implementing web proxy

    It won't compile because of missing classes: HttpRequest HttpResponse

    anything you find wrong in my code.
    It is very pooryly formatted. The placement of {}s and the indentations makes it very hard to read the code.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. My proxy Server
    By hivesh in forum What's Wrong With My Code?
    Replies: 4
    Last Post: February 6th, 2013, 08:09 AM
  2. My proxy Server
    By hivesh in forum What's Wrong With My Code?
    Replies: 0
    Last Post: February 5th, 2013, 01:26 PM
  3. Dealing with a simple web proxy in java
    By lawrey in forum Java Networking
    Replies: 0
    Last Post: October 15th, 2012, 08:25 AM
  4. Replies: 1
    Last Post: September 15th, 2011, 07:50 AM
  5. [SOLVED] Proxy authentication
    By jaya in forum Java Networking
    Replies: 2
    Last Post: January 27th, 2011, 04:04 AM

Tags for this Thread