import java.io.IOException;
import java.net.URL;
import java.net.URLConnection;
public class HttpErrorChecker {
public static void main(String[] args) {
String urlString = "https://www.example.com/nonexistent_page"; // Replace with your URL
try {
URL url = new URL(urlString);
URLConnection connection = url.openConnection();
connection.connect(); // Initiate the connection
//Get the response status code
int statusCode = connection.getResponseCode();
if (statusCode >= 400) {
System.err.println("HTTP Error: " + statusCode); // Print error code
System.err.println("Response: " + connection.getResponseMessage()); //Display message
} else {
System.out.println("Request successful. Status Code: " + statusCode);
}
} catch (IOException e) {
System.err.println("IOException: " + e.getMessage()); //Handle connection errors
}
}
}
Add your comment