Citation :
import java.io.*;
import java.net.*;
class test {
private static Socket socket, socketToWeb;
private static BufferedReader fromBrowser, fromWeb;
private static PrintWriter toBrowser, toWeb;
public static int port = 12345;
private static ServerSocket s;
public static void main(String[] args) throws IOException {
s = new ServerSocket(port);
while (true) {
try {
socket = s.accept();
//ouverture des flux entrants et sortants du socket
fromBrowser = new BufferedReader(new InputStreamReader(socket
.getInputStream()));
toBrowser = new PrintWriter(new BufferedWriter(
new OutputStreamWriter(socket.getOutputStream())), true);
String str, targetHost = "", httpHeader = "";
while (true) {
str = fromBrowser.readLine();
httpHeader += str + "\r\n";
if (str.startsWith("Host: " )) // On recupere l'host pour savoir ou le socket doit se connecter.
targetHost = str.substring(6);
if (str.length() == 0)
break;
}
socketToWeb = new Socket(InetAddress.getByName(targetHost), 80);// connexion d'un nouveau socket au site web
System.out.println("HOST: " + targetHost);
// ouverture des flux entrants et sortants du socket connecte au site web.
fromWeb = new BufferedReader(new InputStreamReader(socketToWeb
.getInputStream()));
toWeb = new PrintWriter(new BufferedWriter(
new OutputStreamWriter(socketToWeb.getOutputStream())),
true);
toWeb.println(httpHeader); // envoi de la requete au site web
String answer = "";
while (true) {
str = fromWeb.readLine();
if (str == null)
break;
answer += (str + "\r\n" );
// reception de la reponse du site web.
}
toBrowser.println(answer); // envoi des données recues au browser.
socket.close();
socketToWeb.close();
} catch (Exception e) {
}
}
}
}
|