ex3 p2
ajouter
This commit is contained in:
54
src/main/java/fr/istic/pr/ping/HttpPing.java
Normal file
54
src/main/java/fr/istic/pr/ping/HttpPing.java
Normal 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;
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user