64

How to verify if a String in Java is a valid URL?

2240
  • 1,547
  • 2
  • 12
  • 30
lacas
  • 13,928
  • 30
  • 109
  • 183

8 Answers8

74

You can try to create a java.net.URL object out of it. If it is not a proper URL, a MalformedURLException will be thrown.

Marc Bannout
  • 388
  • 4
  • 15
Grodriguez
  • 21,501
  • 10
  • 63
  • 107
  • 6
    Just to be clear, it would only throw a MalformedURLException `if no protocol is specified, or an unknown protocol is found`. Unlike, Apache's UrlValidator, no additional validation is performed. – dogbane Oct 14 '10 at 10:44
  • 1
    It actually performs other checks too, e.g. port numbers. – Grodriguez Oct 14 '10 at 10:53
  • 1
    I quoted that from the javadocs http://download.oracle.com/javase/6/docs/api/java/net/URL.html. They don't state any other checks. – dogbane Oct 14 '10 at 11:05
  • 2
    That's what the Javadocs say, however the checks are performed (you can try it yourself, or have a look at the source code if you wish) – Grodriguez Oct 14 '10 at 11:12
  • Just keep in mind that this will throw a false positive if the url is numerical, such as 192.168.0.1 (without the http://). – Tommie Mar 17 '15 at 20:52
  • 1
    @Tommie "192.168.0.1" (without the http://) is not a valid URL. Creating a `java.net.URL` passing this as parameter fails with `java.net.MalformedURLException: no protocol`. This is the expected behaviour and is documented as such. No false positive here. – Grodriguez Mar 23 '15 at 08:13
  • This test fails for the URL `public static void main(String[] argv) throws MalformedURLException { URL url = new URL("http://ProductDetail/2Thumbz, Inc.?source=Admin"); }` because it **doesn't** throw an exception, despite the comma and space and period. – Chloe Jan 27 '19 at 04:56
  • The simplest solution seems to be just using URI class for validation. Also commas/dots in this example (after the auth part) should be valid, only the space is not. – Neikius Apr 15 '19 at 15:24
  • 1
    This is an example of using exceptions as flow control and it's generally considered bad, and for good reasons. Suggested reading: http://archive.fo/erYDY – Danilo Pianini Jul 08 '19 at 08:50
51

You can use UrlValidator from commons-validator. It will save you from writing code where the logic flow is guided by catching an exception, which is generally considered a bad practice. In this case, however, I think it's fine to do as others suggested, if you move this functionality to an utility method called isValidUrl(..)

yiwei
  • 4,022
  • 9
  • 36
  • 54
Bozho
  • 588,226
  • 146
  • 1,060
  • 1,140
19

For Android just add this line:

boolean isValid = URLUtil.isValidUrl( "your.uri" );
pme
  • 14,156
  • 3
  • 52
  • 95
Rafa0809
  • 1,733
  • 21
  • 24
  • This doesn't work if the scheme/prefix (e.g. "http") is missing (e.g. "stackoverflow.com") and it also doesn't work with the "ftp" scheme/prefix (e.g. "ftp://127.0.0.1"). – Neph Mar 17 '20 at 14:43
  • Hence the difference between URL and URI – Rafa0809 Mar 17 '20 at 23:03
  • 1
    I also tried the other suggestions (`new URL`/`new URI`) but `URL` doesn't like URLs without scheme (plus it feels like it only checks for the scheme, nothing else) and `URI` seems to return "true", no matter what you throw at it (even "---"). So far I've not found a way to do what op asked that works properly 99.9% of the time. – Neph Mar 18 '20 at 09:34
15
public static boolean isValidURL(String urlString) {
    try {
        URL url = new URL(urlString);
        url.toURI();
        return true;
    } catch (Exception e) {
        return false;
    }
}

This checks according to RFC2396, like <scheme>://<authority><path>?<query>#<fragment>, but scheme must be known by URL source code. These are not valid:

  • telnet://melvyl.ucop.edu
  • news:comp.infosystems.www.servers.unix
  • www.google.com
Jemshit
  • 9,501
  • 5
  • 69
  • 106
BullyWiiPlaza
  • 17,329
  • 10
  • 113
  • 185
12

If you program in Android, you could use android.webkit.URLUtil to test.

URLUtil.isHttpUrl(url)
URLUtil.isHttpsUrl(url)

Hope it would be helpful.

pme
  • 14,156
  • 3
  • 52
  • 95
alijandro
  • 11,627
  • 2
  • 58
  • 74
  • 12
    This doesnt actually check if its a url it only checks if there is http:// in the beginning, what you need with that is URLUtil.isValidUrl(url) – Amr El Aswar Nov 28 '15 at 00:34
  • 1
    Quick heads up that it's called URLUtil ... not URLUtil*S*. – Tommie Dec 06 '15 at 14:30
11

Complementing Bozho answer, to go more practical:

  1. Download apache commons package and uncompress it. binaries
  2. Include commons-validator-1.4.0.jar in your java build path.
  3. Test it with this sample code (reference):

    //...your imports
    
    import org.apache.commons.validator.routines.*; // Import routines package!
    
    public static void main(String[] args){
    
    // Get an UrlValidator
    UrlValidator defaultValidator = new UrlValidator(); // default schemes
    if (defaultValidator.isValid("http://www.apache.org")) {
        System.out.println("valid");
    }
    if (!defaultValidator.isValid("http//www.oops.com")) {
        System.out.println("INvalid");
    }
    
    // Get an UrlValidator with custom schemes
    String[] customSchemes = { "sftp", "scp", "https" };
    UrlValidator customValidator = new UrlValidator(customSchemes);
    if (!customValidator.isValid("http://www.apache.org")) {
        System.out.println("valid");
    }
    
    // Get an UrlValidator that allows double slashes in the path
    UrlValidator doubleSlashValidator = new UrlValidator(UrlValidator.ALLOW_2_SLASHES);
    if (doubleSlashValidator.isValid("http://www.apache.org//projects")) {
        System.out.println("INvalid");
    }
    
  4. Run/Debug

ConcurrentHashMap
  • 4,998
  • 7
  • 44
  • 53
Esteban Cacavelos
  • 753
  • 10
  • 22
3

This function validates a URL, and returns true (valid URL) or false (invalid URL).

public static boolean isURL(String url) {
    try {
        new URL(url);
        return true;
    } catch (Exception e) {
        return false;
    }
}

Be sure to import java.net.URL;

Aaron Esau
  • 1,083
  • 3
  • 15
  • 31
  • 3
    Doesn't fail for `http://ProductDetail/2Thumbz, Inc.?source=Admin` when it should (comma, space, period). – Chloe Jan 27 '19 at 04:57
1
^(https://|http://|ftp://|ftps://)(?!-.)[^\\s/\$.?#].[^\\s]*$

This is a regex to validate URL for protocols http, https, ftp and ftps.

Paul Roub
  • 36,322
  • 27
  • 84
  • 93