Skip to main content

Validating URL in Java


I wanted to know if there is any standard APIs in Java to validate a given URL? I want to check both if the URL string is right i.e. the given protocol is valid and then to check if a connection can be established.



I tried using HttpURLConnection, providing the URL and connecting to it. The first part of my requirement seems to be fulfilled but when I try to perform HttpURLConnection.connect(), 'java.net.ConnectException: Connection refused' exception is thrown.



Can this be because of proxy settings? I tried setting the System properties for proxy but no success.



Let me know what I am doing wrong.


Source: Tips4allCCNA FINAL EXAM

Comments

  1. You need to create both a URL object and a URLConnection object. The following code will test both the format of the URL and whether a connection can be established:

    try {
    URL url = new URL("http://www.yoursite.com/");
    URLConnection conn = url.openConnection();
    conn.connect();
    } catch (MalformedURLException e) {
    // the URL is not in a valid form
    } catch (IOException e) {
    // the connection couldn't be established
    }

    ReplyDelete
  2. For the benefit of the community, since this thread is third on Google when searching for "url validator java":
    Catching exceptions is expensive, and should be avoided when possible. If you just want to verify your String is valid URL, you can use Apache commons-validator URLValidator class. For example:

    String[] schemes = {"http","https"}.
    UrlValidator urlValidator = new UrlValidator(schemes);
    if (urlValidator.isValid("ftp://foo.bar.com/")) {
    System.out.println("url is valid");
    } else {
    System.out.println("url is invalid");
    }

    ReplyDelete
  3. http://java.sun.com/j2se/1.4.2/docs/api/java/net/URL.html

    If the constructor throws an exception => URL invalid

    ReplyDelete
  4. Are you sure you're using the correct proxy as system properties?

    Also if you are using 1.5 or 1.6 you could pass a java.net.Proxy instance to the openConnection() method. This is more elegant imo:

    //Proxy instance, proxy ip = 10.0.0.1 with port 8080
    Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress("10.0.0.1", 8080));
    conn = new URL(urlString).openConnection(proxy);

    ReplyDelete
  5. The java.net.URL class is in fact not at all a good way of validating URLs. MalformedURLException is not thrown on all malformed URLs during construction. Catching IOException on java.net.URL#openConnection().connect() does not validate URL either, only tell wether or not the connection can be established.

    Consider this piece of code:

    try {
    new URL("http://.com");
    new URL("http://com.");
    new URL("http:// ");
    new URL("ftp://::::@example.com");
    } catch (MalformedURLException malformedURLException) {
    malformedURLException.printStackTrace();
    }


    ..which does not throw any exceptions.

    I recommend using some validation API implemented using a context free grammar, or in very simplified validation just use regular expressions. However I need someone to suggest a superior or standard API for this, I only recently started searching for it myself.

    ReplyDelete
  6. Just important to point that the URL object handle both validation and connection. Then, only protocols for which a handler has been provided in sun.net.www.protocol are authorized (file,
    ftp, gopher, http, https, jar, mailto, netdoc) are valid ones. For instance, try to make a new URL with the ldap protocol:

    new URL("ldap://myhost:389")

    You will get a java.net.MalformedURLException: unknown protocol: ldap.

    You need to implement your own handler and register it through URL.setURLStreamHandlerFactory(). Quite overkill if you just want to validate the URL syntax, a regexp seems to be a simpler solution.

    ReplyDelete
  7. Thanks. Opening the URL connection by passing the Proxy as suggested by NickDK works fine.

    //Proxy instance, proxy ip = 10.0.0.1 with port 8080
    Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress("10.0.0.1", 8080));
    conn = new URL(urlString).openConnection(proxy);

    System properties however doesn't work as I had mentioned earlier.

    Thanks again.

    Regards,
    Keya

    ReplyDelete
  8. This url shows as being invalid when attempting to open the connection
    Validating URL in Java

    ReplyDelete

Post a Comment

Popular posts from this blog

Slow Android emulator

I have a 2.67 GHz Celeron processor, 1.21 GB of RAM on a x86 Windows XP Professional machine. My understanding is that the Android emulator should start fairly quickly on such a machine, but for me it does not. I have followed all instructions in setting up the IDE, SDKs, JDKs and such and have had some success in staring the emulator quickly but is very particulary. How can I, if possible, fix this problem?