0

I have a specific link, something like https://wiki.dummy.com/absolute/path/of/webpage and I want to check if this URL is valid. Obviously pinging the complete URL is not an option, and though pinging wiki.dummy.com works, that doesn't prove that the URL is valid. Is there any other way I could quickly validate and then implement it as Java code?

UPDATE: I found this answer working for me- https://stackoverflow.com/a/18135030/1649068

Community
  • 1
  • 1
AlwaysALearner
  • 6,320
  • 15
  • 44
  • 59
  • possible duplicate of [Validating URL in Java](http://stackoverflow.com/questions/1600291/validating-url-in-java) – Santhosh Jan 27 '15 at 09:31
  • @SanKrish I'm not sure if I'm looking at the right question you mentioned. Please help me out here. The URL I'm specifying does not end with `.com`, that's why I mentioned the complete URL. Does that mean it still works in my scenario too? I'm asking this because the link you provided has examples that end with `.com` – AlwaysALearner Jan 27 '15 at 09:37

1 Answers1

0

You mean syntactically?
There are several ways to check using regular expressions. Just google it. One way could be:

if (url.matches("^http(s{0,1})://[a-zA-Z0-9_/\\-\\.]+([:][0-9]+)?[a-zA-Z0-9_/\\&\\?\\=\\-\\.\\~\\%]*")) 
    System.out.println("URL is valid");
else 
    System.out.println("NOT valid");

Or you mean that URL exists?
You could use new URL(url).openConnection() or use Apache HttpClient 3.x or Apache HttpClient 4.x, now part of Apache Components

HttpClient httpClient = new HttpClient(); 
HttpMethod method = new PostMethod(url); 
Albert
  • 1,156
  • 1
  • 15
  • 27