views:

235

answers:

1

I am looking for a java library/class to parse clean URL's and get all the properties like query, port, host , domain, subdomain etc. Essentially most of the functionality that java.net.URI does but even for clean URLs. I am guessing since this is a pretty common requirement, there might be some libraries already built to handle this. Help?

+4  A: 

java.net.URL class has methods getHost(), getPort(), getQuery(), getPath().

You can also look at URI class, it's more preferred to use it.

Roman
i would recommend using the URI class since the URL.equals() method actually needs a live connection and does a DNS lookup every time the equals() method is called. so it could be hell for testing in a non networked environment. cherck here: http://brian.pontarelli.com/2006/12/05/mr-gosling-why-did-you-make-url-equals-suck/
smeg4brains
Nice suggestion. I also watched Josh Bloch presentation on youtube about it but didn't add it to my answer 'cause it's especially matters when URL/URI objects are elements of Set or keys in Map, and it's not the case imho.
Roman
I am currently using java.net.URL but it doesn't work all that well with Clean URL's. Are there anythings targeted specifically at clean URL libraries?
Ritesh M Nayak
I'd suggest you to use regular expressions for further parsing. You need one to get list of subdomains, maybe one more to get domain name. Don't know what else you can get from url (if you don't want to implement some tricky logic of course).
Roman