Le protocole HTTP: Proxy HTTP Java


- Ce programme est ecrit en JAVA - Lancez le, configurer votre navigateur pour l'utiliser comme proxy - Vous pourrez alors espionner tous les messages qui sont echangés entre les deux machines ( serveur Web et navigateur Web )
import java.io.*;
import java.net.*;

class OneServeur extends Thread{
  private Socket socket, socketToWeb;
  private BufferedReader fromBrowser;
  private PrintWriter toBrowser;
  private PrintWriter toWeb;
  private BufferedReader fromWeb;

  public OneServeur(Socket s) throws IOException {
        socket = s;
        fromBrowser = new BufferedReader(
                            new InputStreamReader (
                              socket.getInputStream()));
        toBrowser = new PrintWriter( new BufferedWriter (
                                      new OutputStreamWriter (
                                      socket.getOutputStream())),true);
        start();
  }

  public void run() {
    try {
        String str, targetHost="", httpHeader="";
        while(true) {
             str = fromBrowser.readLine();
             httpHeader += str + "\n";
             if ( str.startsWith("Host: " ) ) targetHost = str.substring(6);
             if ( str.length() == 0 ) break;
        }
        //socketToWeb = new Socket(InetAddress.getByName(targetHost),80);
        socketToWeb = new Socket(
                            InetAddress.getByName("your.real.proxy.com"),80);
        System.out.println(socketToWeb);
        fromWeb = new BufferedReader(
                      new InputStreamReader (
                          socketToWeb.getInputStream()));
        toWeb = new PrintWriter( new BufferedWriter (
                                  new OutputStreamWriter (
                                    socketToWeb.getOutputStream())),true);
        System.out.println("-> -> -> -> -> -> ->  \n" + httpHeader);
        toWeb.println(httpHeader);


        boolean echoing = true;

        while ( true ) {
             str = fromWeb.readLine();
             if ( str == null ) break;
             if ( str.length() == 0 ) echoing = false;
             if( echoing ) System.out.println(str);
             toBrowser.println(str);
        }
        System.out.println("\nclosing ...");
    } catch ( IOException e) {
    } finally {
        try {
                socket.close();
        } catch ( IOException e ) {}
    }
  }
}

public class Proxy {

  public static final int PORT = 12345;

  public static void main(String args[]) throws IOException {
        ServerSocket s = new ServerSocket(PORT);
        System.out.println("Started " + s);

        try {
                while ( true ) {
                        Socket socket = s.accept();
                        try {
                                System.out.println( "connection acceptee " +
                                                    socket);
                                new OneServeur(socket);
                        } catch ( IOException e ) {
                                socket.close();
                        }
                }
        } finally {
                s.close();
        }
  }
}