partie 2 exo 3 fini

ok
This commit is contained in:
Minh VU
2025-02-04 23:20:42 +01:00
parent 3ba5b30a6e
commit bfa7cd812d

View File

@@ -1,7 +1,6 @@
package fr.istic.pr.ping;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.*;
import java.net.Socket;
import java.net.UnknownHostException;
@@ -24,28 +23,42 @@ public class HttpPing {
}
public static PingInfo ping(String host, int port) throws UnknownHostException, IOException {
public static PingInfo ping(String host, int port) throws UnknownHostException, IOException, InterruptedException {
PingInfo info = new PingInfo();
long time = System.currentTimeMillis();
Socket socket = new Socket(host, port);
PrintWriter out = new ...
// UTILISER PrintWriter et BufferedReader
PrintWriter out = new PrintWriter(new OutputStreamWriter(socket.getOutputStream()));
BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
/// Envoyer l'entête
//https://stackoverflow.com/questions/9405620/send-http-header-for-identification
out.println("GET / HTTP/1.1");
// Penser à préciser l'Host dans l'en-tête
out.println("Host: " + host);
// L'en-tête doit également contenir :
out.println("Connection: close"); //demande au site de fermer la connexion après la réponse.
// et se terminier par l'envoie d'une ligne vide sans espace
out.println();
out.flush();
// LECTURE DE LA REPONSE
String line;
while ((line=in.readLine()) != null) {
String[] ligne =line.split(" ");
if (ligne[0].equals("HTTP/1.1")){
info.code=Integer.parseInt(ligne[1]);
break;
}
}
//info.code = utiliser split sur la première ligne pour parser le code
//AFFICHAGE DE LA PAGE
System.out.println(line); //1ère ligne
while ((line=in.readLine()) != null) {
System.out.println(line);
}
info.time = System.currentTimeMillis() - time;
return info;