Created
June 4, 2009 23:20
-
-
Save mfcabrera/123925 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.io.IOException; | |
import java.net.ConnectException; | |
import java.net.HttpURLConnection; | |
import java.net.MalformedURLException; | |
import java.net.SocketTimeoutException; | |
import java.net.URL; | |
import java.net.URLConnection; | |
import java.net.UnknownHostException; | |
/** | |
* | |
* @author Miguel Cabrera | |
*/ | |
public class FrakaCheckURLsTest { | |
//static String urlString = "http://almbpaix04.grupoargos.loc:7777/BPELConsole/"; | |
static String urlString = "http://localhost:8084/BorrarWeb500/Excepcion"; | |
public static void main(String[] args) throws MalformedURLException { | |
HttpURLConnection.setFollowRedirects(false); | |
URL url = new URL(urlString); | |
try { | |
URLConnection connection = url.openConnection(); | |
if (connection instanceof HttpURLConnection) { | |
HttpURLConnection httpConnection = (HttpURLConnection) connection; | |
httpConnection.setRequestMethod("HEAD"); // Para no recibir los datos HTML, solo las cabeceras HTTP | |
httpConnection.setRequestProperty("Connection", "Close"); // Para que cierre la conexion TCP despues de recibir la respuesta HTTP | |
httpConnection.setConnectTimeout(10000); // En milisegundos, desde 1.5 | |
httpConnection.setReadTimeout(10000); // En milisegundos, desde 1.5 | |
httpConnection.connect(); // Hasta aqui, solo la conexion TCP | |
int response = httpConnection.getResponseCode(); // Aqui la conexion HTTP | |
System.out.println("Codigo de respuesta = " + response); | |
// Pruebas | |
// response = 200 : OK | |
// response = 404 : OK (el documento no existe) | |
// response = 500 : OK (internal server error) | |
} | |
} catch (UnknownHostException uhe) { // java.net.UnknownHostException <-- java.io.IOException <-- java.lang.Exception | |
// Cuando el host no existe | |
// lanzado por httpConnection.connect() | |
// Imprime algo bonito cuando pase esto | |
System.out.println("Te equivocaste de host cabron! = "); | |
} catch (ConnectException ce) { // // java.net.ConnectException <-- java.net.SocketException <-- java.io.IOException <-- java.lang.Exception | |
//------------------------------------------------------------------ | |
// Connection refused | |
// Cuando el puerto remoto no esta abierto | |
// Lanzado por httpConnection.connect() | |
// getMessage = "Connection refused: connect" | |
// toString = "java.net.ConnectException: Connection refused: connect" | |
//------------------------------------------------------------------ | |
System.out.println(ce); | |
} catch (SocketTimeoutException ste) { // java.net.SocketTimeoutException <-- java.io.InterruptedIOException <-- java.io.IOException <-- java.lang.Exception | |
//------------------------------------------------------------------ | |
// Read timeout | |
// Cuando no ha terminado de recibir la respuesta HTTP en "ReadTimeout" milisegundos | |
// Lanzado por httpConnection.getResponseCode() | |
// getMessage = "Read timed out" | |
// toString = "java.net.SocketTimeoutException: Read timed out" | |
//------------------------------------------------------------------ | |
//------------------------------------------------------------------ | |
// Socket timeout | |
// Cuando no se ha podido conectar (TCP) en setConnectTimeout | |
// httpConnection.connect( | |
// getMessage = "connect timed out" | |
// toString = "java.net.SocketTimeoutException: connect timed out" | |
//------------------------------------------------------------------ | |
System.out.println(ste); | |
} catch (IOException ioe) { | |
// Que otros casos se pueden dar | |
System.out.println(ioe); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment