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

Why is this Javascript much *slower* than its jQuery equivalent?

I have a HTML list of about 500 items and a "filter" box above it. I started by using jQuery to filter the list when I typed a letter (timing code added later): $('#filter').keyup( function() { var jqStart = (new Date).getTime(); var search = $(this).val().toLowerCase(); var $list = $('ul.ablist > li'); $list.each( function() { if ( $(this).text().toLowerCase().indexOf(search) === -1 ) $(this).hide(); else $(this).show(); } ); console.log('Time: ' + ((new Date).getTime() - jqStart)); } ); However, there was a couple of seconds delay after typing each letter (particularly the first letter). So I thought it may be slightly quicker if I used plain Javascript (I read recently that jQuery's each function is particularly slow). Here's my JS equivalent: document.getElementById('filter').addEventListener( 'keyup', function () { var jsStart = (new Date).getTime()...

Is it possible to have IF statement in an Echo statement in PHP

Thanks in advance. I did look at the other questions/answers that were similar and didn't find exactly what I was looking for. I'm trying to do this, am I on the right path? echo " <div id='tabs-".$match."'> <textarea id='".$match."' name='".$match."'>". if ($COLUMN_NAME === $match) { echo $FIELD_WITH_COLUMN_NAME; } else { } ."</textarea> <script type='text/javascript'> CKEDITOR.replace( '".$match."' ); </script> </div>"; I am getting the following error message in the browser: Parse error: syntax error, unexpected T_IF Please let me know if this is the right way to go about nesting an IF statement inside an echo. Thank you.