You have two problems, only one of which you've already encountered.
1. Don't use URL
!
The URL
class does some weird and unexpected things that you basically never want. For example, the URL.equals
method states (emphasis mine):
Two hosts are considered equivalent if both host names can be resolved into the same IP addresses [...]
Since hosts comparison requires name resolution, this operation is a blocking operation.
Note: The defined behavior for equals is known to be inconsistent with virtual hosting in HTTP.
Use URI
instead. It's docs describe a few other shortcomings of the URL
class, including:
Not all URIs can be represented as URLs:
Comparison is not defined.
URL.equals
and URL.hashCode
both block while they consult the Internet.
Object equality (and hash codes) can vary based on your DNS setup... Two "equal" URL
objects on one machine might be un-equal on another.
Yikes.
2. Your expectations are wrong.
There is nothing really wrong with a URI like "http:sdfasdfasdfas". It will even work in many browsers... if you happen to have a local host named "sdfasdfasdfas", and it serves Web pages.
The URI
class docs, under "URI syntax and components", define URIs as made up of the following parts:
[scheme:
]scheme-specific-part[#
fragment]
Your example "http:sdfasdfasdfas" has a scheme, making it an "absolute URI". It also has a scheme-specific part, but no fragment. Regarding the scheme-specific part...
An opaque URI is an absolute URI whose scheme-specific part does not begin with a slash character ('/'
). Opaque URIs are not subject to further parsing. Some examples of opaque URIs are:
- mailto:java-net@java.sun.com
- news:comp.lang.java
- urn:isbn:096139210x
Your example is an opaque URI, and its scheme-specific part may be almost anything, including that weird "hostname".
Your other examples are also valid URIs, with one exception:
"http:" would be an absolute opaque URI, but it's missing the required scheme-specific part. ("" isn't good enough).
"http:/" is an absolute hierarchical URI with scheme "http:" and path "/".
"http:/rubbish" is the same, but with the path "/rubbish".
If you wanted the URI
class (or the URL
class, if you insist) to verify opaque URIs for you, it would have to "know" how valid scheme-specific parts are defined for all schemes... including ones that don't exist yet.
Conclusion
You can declare valid URIs like your example(s) to be invalid if you really want, but you'll probably have to code something of your own to throw a MalformedURLException
, or preferably your own more specific exception.
I think you'd be better off accepting the definition of "URI" that the rest of the world uses, and spending your time fixing whatever code is choking on valid URIs.