Home > database >  How to make sure URLConnection is valid and will successfully connect in Java?
How to make sure URLConnection is valid and will successfully connect in Java?

Time:12-04

how can you ensure URL you receive from users is a valid url and not just a http://nothing.com

my code looks like this:

String urlFromUser = getUrlFromUser(); // might return: http://www.notARealSite.com
URL url = new URL(urlFromUser);

// this might fail
URLConnection urlConnection = url.openConnection();

and try catch is not enough, i want to make sure the site is real

CodePudding user response:

One way to check if a URL is valid is to use the java.net.URL class to attempt to create a URL object from the user-provided input. If the URL is invalid, the URL constructor will throw a MalformedURLException. You can catch this exception and handle it accordingly.

Here's an example of how you might implement this:

String urlFromUser = getUrlFromUser(); // might return: http://www.notARealSite.com

try {
    URL url = new URL(urlFromUser);
    URLConnection urlConnection = url.openConnection();
    // If we reached this point, it means the URL is valid and we can use the connection
} catch (MalformedURLException e) {
    // The URL is not valid. You can either show an error message to the user or 
    // try to fix the URL and create a new URL object.
}

Another option is to use the java.net.HttpURLConnection class to try to connect to the URL and check the response code. If the response code is in the range 200-299, it means the connection was successful and the URL is valid. Here's an example of how you might do this:

String urlFromUser = getUrlFromUser(); // might return: http://www.notARealSite.com

try {
    URL url = new URL(urlFromUser);
    HttpURLConnection connection = (HttpURLConnection) url.openConnection();
    int responseCode = connection.getResponseCode();

    if (responseCode >= 200 && responseCode < 300) {
        // The connection was successful and the URL is valid
    } else {
        // The connection was not successful. The URL might be invalid or there might be
        // some other issue with the connection (e.g. the server is down).
    }
} catch (MalformedURLException e) {
    // The URL is not valid. You can either show an error message to the user or 
    // try to fix the URL and create a new URL object.
} catch (IOException e) {
    // There was an IO error while trying to open the connection. This could be caused by
    // various factors, such as network issues or issues with the server.
}

It's important to note that even if the URL is valid and the connection is successful, there's no guarantee that the website or server at the other end of the connection will return the expected content or behave as expected. You'll need to handle any errors or unexpected behavior that might occur when using the connection.

CodePudding user response:

you can also use:

if(new InetSocketAddress(urlFromUser, 80).isUnresolved()) {
    // URL is a not a valid server address
}
else {
    // URL is valid
}
  • Related