URL validation in Java

I wanted to know if Java has standard APIs to validate this URL? I want to check if the URL string is correct, i.e. This protocol is valid, and then check whether the connection can be established.

I tried using HttpURLConnection by specifying the url and connecting to it. The first part of my request seems to be fulfilled, but when I try to execute HttpURLConnection.connect (), an exception is thrown "java.net.ConnectException: Connection refused".

Could this be due to the proxy server settings? I tried to set the System properties for the proxy, but did not succeed.

Let me know what I'm doing wrong.

+81
java url validation
Oct. 21 '09 at 11:38
source share
9 answers

It’s in the interest of the community, since this thread is the pinnacle of Google’s search on the Internet for β€œ validating the java validator ”




Catching exceptions is expensive and should be avoided whenever possible. If you just want to verify that your string is a valid URL, you can use the UrlValidator class from the Apache Commons Validator Project .

For example:

String[] schemes = {"http","https"}; // DEFAULT schemes = "http", "https", "ftp" 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"); } 
+121
Feb 22 2018-11-22T00:
source share

You need to create a URL object and a URLConnection object. The following code will check both the format of the URL and the ability to establish a connection:

 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 } 
+34
Oct 21 '09 at 11:47
source share

The java.net.URL class is actually not a good way to validate URLs. MalformedURLException not imposed on all invalid URLs at build time. The IOException trap on java.net.URL#openConnection().connect() does not check the URL, just let it know 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 context free grammar, or just use regular expressions in a very simplified validation. However, I need someone to offer an excellent or standard API for this, I just recently started looking for it myself.

Note It has been suggested that the URL#toURI() is combined with java.net. URISyntaxException exception handling java.net. URISyntaxException java.net. URISyntaxException may facilitate URL checking. However, this method only catches one of the simplest cases above.

The conclusion is that there is no standard java URL parser for checking URLs.

+27
May 11 '11 at 14:18
source share

Using the standard API only , pass the string to the URL object, then convert it to a URI object. This will accurately determine the correct URL in accordance with the RFC2396 standard.

Example:

 public boolean isValidURL(String url) { URL u = null; try { u = new URL(url); } catch (MalformedURLException e) { return false; } try { u.toURI(); } catch (URISyntaxException e) { return false; } return true; } 
+9
Jul 27 '13 at 5:30
source share

Use android.webkit.URLUtil for Android:

 URLUtil.isValidUrl(URL_STRING); 

Note. It just checks the source URL scheme, not the entire URL.

+6
Dec 18 '15 at 15:41
source share

Are you sure you are using the correct proxy as system properties?

Also, if you use 1.5 or 1.6, you can pass an instance of java.net.Proxy for 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); 
+2
Oct 21 '09 at 11:47
source share

It is simply important to indicate that the URL object handles both validation and connection. Then only the protocols for which the handler was provided in sun.net.www.protocol (file, ftp, gopher, http, https, jar, mailto, netdoc) are valid are allowed. For example, try creating a new URL using the ldap protocol:

 new URL("ldap://myhost:389") 

You will get java.net.MalformedURLException: unknown protocol: ldap .

You need to implement your own handler and register it through URL.setURLStreamHandlerFactory() . Enough is enough if you just want to check the syntax of a URL, regexp seems to be a simpler solution.

+1
Feb 04 '11 at 10:50
source share

There is a way to check URLs in strict accordance with Java standards without resorting to third-party libraries:

 boolean isValidURL(String url) { try { new URI(url).parseServerAuthority(); return true; } catch (URISyntaxException e) { return false; } } 

The URI constructor checks that the url is a valid URI, and calling parseServerAuthority ensures that it is a URL (absolute or relative), not a URN.

+1
Aug 08 '17 at 15:57
source share

Thank. Opening a URL connection by passing a proxy server, as NickDK suggested, works great.

 //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); 

However, the properties of the system do not work, as I mentioned earlier.

Thanks again.

Regards, Kei

0
Oct. 21 '09 at 11:55
source share



All Articles