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

[韓日関係] 首相含む大幅な内閣改造の可能性…早ければ来月10日ごろ=韓国

div not scrolling properly with slimScroll plugin

I am using the slimScroll plugin for jQuery by Piotr Rochala Which is a great plugin for nice scrollbars on most browsers but I am stuck because I am using it for a chat box and whenever the user appends new text to the boxit does scroll using the .scrollTop() method however the plugin's scrollbar doesnt scroll with it and when the user wants to look though the chat history it will start scrolling from near the top. I have made a quick demo of my situation http://jsfiddle.net/DY9CT/2/ Does anyone know how to solve this problem?

Why does this javascript based printing cause Safari to refresh the page?

The page I am working on has a javascript function executed to print parts of the page. For some reason, printing in Safari, causes the window to somehow update. I say somehow, because it does not really refresh as in reload the page, but rather it starts the "rendering" of the page from start, i.e. scroll to top, flash animations start from 0, and so forth. The effect is reproduced by this fiddle: http://jsfiddle.net/fYmnB/ Clicking the print button and finishing or cancelling a print in Safari causes the screen to "go white" for a sec, which in my real website manifests itself as something "like" a reload. While running print button with, let's say, Firefox, just opens and closes the print dialogue without affecting the fiddle page in any way. Is there something with my way of calling the browsers print method that causes this, or how can it be explained - and preferably, avoided? P.S.: On my real site the same occurs with Chrome. In the ex