ajouter
This commit is contained in:
Minh VU
2025-02-04 17:42:39 +01:00
parent 4c68c22c96
commit 3ba5b30a6e
2 changed files with 60 additions and 1 deletions

View File

@@ -0,0 +1,54 @@
package fr.istic.pr.ping;
import java.io.IOException;
import java.io.PrintWriter;
import java.net.Socket;
import java.net.UnknownHostException;
public class HttpPing {
public static class PingInfo {
//Le temps de réponse :
long time;
//Le code de réponse :
int code;
@Override
public String toString() {
return String.format("[code %d -- %d ms]", code, time);
}
}
public static void main(String[] args) throws UnknownHostException, IOException, InterruptedException {
System.out.println(ping("www.example.com", 80));
}
public static PingInfo ping(String host, int port) throws UnknownHostException, IOException {
PingInfo info = new PingInfo();
long time = System.currentTimeMillis();
Socket socket = new Socket(host, port);
PrintWriter out = new ...
// UTILISER PrintWriter et BufferedReader
/// Envoyer l'entête
// Penser à préciser l'Host dans l'en-tête
// 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
// LECTURE DE LA REPONSE
//info.code = utiliser split sur la première ligne pour parser le code
//AFFICHAGE DE LA PAGE
info.time = System.currentTimeMillis() - time;
return info;
}
}