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.
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:
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.You need to create both a
URL
object and aURLConnection
object. The following code will test both the format of the URL and whether a connection can be established:Using only standard API, pass the string to a
URL
object then convert it to aURI
object. This will accurately determine the validity of the URL according to the RFC2396 standard.Example:
There is a way to perform URL validation in strict accordance to standards in Java without resorting to third-party libraries:
The constructor of
URI
checks thaturl
is a valid URI, and the call toparseServerAuthority
ensures that it is a URL (absolute or relative) and not a URN.Thanks. Opening the URL connection by passing the Proxy as suggested by NickDK works fine.
System properties however doesn't work as I had mentioned earlier.
Thanks again.
Regards, Keya
For the benefit of the community, since this thread is top 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 a valid URL, you can use the UrlValidator class from the Apache Commons Validator project.
For example: