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(); } } }