E
ebby83
Hi ppl ...
I am writing a simple proxy server ...
I redirect my browser to the local server and re-route the HTTP
request to the host.
I get a reply back from the HOST but unfortunately this is not shown in
my browser ( html appears in my console ) .
I am putting the code here .. ( Its a thread ) pls run with the Server
Class specified below it ..
package com.webproxy.fthread;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.iutputStreamWriter;
import java.io.PrintWriter;
import java.net.Socket;
import java.net.UnknownHostException;
public class fthread extends Thread {
Socket orig;
Socket dest;
public fthread(Socket s) {
this.orig = s;
}
public void run() {
String line;
try {
BufferedReader r = new BufferedReader(new InputStreamReader(orig
.getInputStream()));
PrintWriter w = new PrintWriter(new OutputStreamWriter(orig
.getOutputStream()));
BufferedReader r2 = null;
PrintWriter w2 = null;
while(true){
line = r.readLine();
if(line.equals(null) || line.equals(""))
break;
System.out.println( line );
if (line.indexOf("GET") > -1) {
String deststr = line.split(" ")[1].split("/")[2]; //
System.out.println(deststr);
dest = new Socket(deststr, 80);
w2 = new PrintWriter(new OutputStreamWriter(dest
.getOutputStream()));
r2 = new BufferedReader(new InputStreamReader(dest
.getInputStream()));
}
w2.write(line + "\n");
w2.flush();
}
w2.write("\n\n");
w2.flush();
System.out.println("\nNow Reading...\n\n" );
while ((line = r2.readLine()) != null) {
System.out.println(line);
w.write(line);
w.flush();
}
w.close();
r2.close();
r.close();
w2.close();
} catch (IOException e) { // TODO Auto-generated catch
e.printStackTrace();
}
}
}
package webproxy.com.webproxy.main;
import java.io.IOException;
import java.iutputStreamWriter;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
import webproxy.com.webproxy.fthread.fthread;
public class ServerClass {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
try {
ServerSocket ss = new ServerSocket(8080);
Socket s = new Socket("localhost", 8080);
Socket s2 = null;
(new PrintWriter(new OutputStreamWriter(s.getOutputStream())))
.write("GET http://www.google.com/ig HTTP/1.0");
s2 = ss.accept();
fthread t = new fthread(s2);
t.start();
t.join();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
I am writing a simple proxy server ...
I redirect my browser to the local server and re-route the HTTP
request to the host.
I get a reply back from the HOST but unfortunately this is not shown in
my browser ( html appears in my console ) .
I am putting the code here .. ( Its a thread ) pls run with the Server
Class specified below it ..
package com.webproxy.fthread;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.iutputStreamWriter;
import java.io.PrintWriter;
import java.net.Socket;
import java.net.UnknownHostException;
public class fthread extends Thread {
Socket orig;
Socket dest;
public fthread(Socket s) {
this.orig = s;
}
public void run() {
String line;
try {
BufferedReader r = new BufferedReader(new InputStreamReader(orig
.getInputStream()));
PrintWriter w = new PrintWriter(new OutputStreamWriter(orig
.getOutputStream()));
BufferedReader r2 = null;
PrintWriter w2 = null;
while(true){
line = r.readLine();
if(line.equals(null) || line.equals(""))
break;
System.out.println( line );
if (line.indexOf("GET") > -1) {
String deststr = line.split(" ")[1].split("/")[2]; //
System.out.println(deststr);
dest = new Socket(deststr, 80);
w2 = new PrintWriter(new OutputStreamWriter(dest
.getOutputStream()));
r2 = new BufferedReader(new InputStreamReader(dest
.getInputStream()));
}
w2.write(line + "\n");
w2.flush();
}
w2.write("\n\n");
w2.flush();
System.out.println("\nNow Reading...\n\n" );
while ((line = r2.readLine()) != null) {
System.out.println(line);
w.write(line);
w.flush();
}
w.close();
r2.close();
r.close();
w2.close();
} catch (IOException e) { // TODO Auto-generated catch
e.printStackTrace();
}
}
}
package webproxy.com.webproxy.main;
import java.io.IOException;
import java.iutputStreamWriter;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
import webproxy.com.webproxy.fthread.fthread;
public class ServerClass {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
try {
ServerSocket ss = new ServerSocket(8080);
Socket s = new Socket("localhost", 8080);
Socket s2 = null;
(new PrintWriter(new OutputStreamWriter(s.getOutputStream())))
.write("GET http://www.google.com/ig HTTP/1.0");
s2 = ss.accept();
fthread t = new fthread(s2);
t.start();
t.join();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}