Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 397
  • Last Modified:

Is there a Java method to validate a String as an valid url?

Is there a method in the Java API (I'm using Java 6) that can be used for validating if a String is a valid URL address? Alternatively is there an easy way to do this yourself? I must admit I'm not sure of the conventions that have to be followed in order for an address to be valid, so I'm not capable myself.
0
Suppai
Asked:
Suppai
  • 6
  • 4
  • 2
  • +3
3 Solutions
 
CEHJCommented:
The easiest way to validate it is to try to connect to it
0
 
objectsCommented:
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
beekeepCommented:
If you are just looking to see whether the syntax is correct (rather than whether the site is reachable), you probably want to use java.net.URI.  java.net.URL would do similar checking, but it has a nasty tendency to actually connect to the network and look up the IP address, which makes it a lot slower without adding any extra benefit.
0
 
CEHJCommented:
>>but it has a nasty tendency to actually connect to the network and look up the IP address

What makes you think that?
0
 
beekeepCommented:
CEHJ: Mostly I know from long and painful experience.  However, you can confirm it by carefully reading the documentation for URL.equals.  It includes the following:

Two URL objects are equal if they have the same protocol, reference
equivalent hosts, have the same port number on the host, and the same
file and fragment of the file.

Two hosts are considered equivalent if both host names can be resolved
into the same IP addresses; else if either host name can't be
resolved, the host names must be equal without regard to case; or both
host names equal to null.

Since hosts comparison requires name resolution, this operation is a
blocking operation.
It's amazing how easy it is to miss this nasty little tidbit.
0
 
beekeepCommented:
Basically, my rule of thumb is:
 * If you want to connect to the network, use a URL.
 * If you want to examine and manipulate the description of a network location, use a URI.
0
 
CEHJCommented:
Well using the URL ctor doesn't perform any connection at all - just some pretty crude string parsing. Having said that, it won't perform any validation anyway since:

"No validation of the inputs is performed by this constructor."

is specified effectively for all ctors. The same goes for a URI, but the latter has much stricter parsing

0
 
beekeepCommented:
CEHJ:  I agree that simple construction of a URL doesn't do a network connection.  But once you get into the habit of using them, it's easy to forget the gotchas and start comparing them or dropping them into hash tables, which is where you get into trouble.  Becoming familiar with URI will save you some heartache.

I said from the start that my suggested use of URI was just for syntax checking, not for determining whether the address actually corresponds to a document.  There seem to be several competing interpretations of "validate", and I'm guessing that Suppai simply wants syntactic checking.  If not, then obviously the best method is to create a URL and try connecting to it.  For syntax checking, I'd say that URI looks to be pretty thorough.

0
 
CEHJCommented:
Yes, syntax checking in URI beats that in URL hands down
0
 
objectsCommented:
Yes you can use either the URL or URI classes to validate your url.
If you want to use URI parsing then just change URL to URI in the code I posted above.

0
 
Kamaraj SubramanianApplication Support AnalystCommented:
why cant you try regular expression ?

check out here

http://leghumped.com/blog/2008/11/03/java-matching-urls-with-regex-wildcards/
0
 
SuppaiAuthor Commented:
I realize I didnt make myself clear. What I wanted was simply a syntax check in order to catch user input errors in the gui. In another part of the app I will undoubtedly need some more complete URL checking, but for this purpose I think I will stick to the URI class. Thanks for the help.
0
 
CEHJCommented:
If all you're interested in is syntactical correctness, you'll find a solid and sophisticated regex more rigorous in parsing
0

Featured Post

Become an Android App Developer

Ready to kick start your career in 2018? Learn how to build an Android app in January’s Course of the Month and open the door to new opportunities.

  • 6
  • 4
  • 2
  • +3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now