Class URL
java.net.URL.
The compiler substitutes java.net.URL for this one, so a library that opens a connection
the ordinary way works unchanged. What it replaces is a version wired to XMLHttpRequest,
which this platform does not have: the class library's own java.net.URL compiles and then
fails on the first request. Underneath this one is fetch.
Parsing is the platform's, which is the WHATWG algorithm rather than the one the JDK uses. The difference shows on inputs neither is asked for in practice, and it is what makes a relative spec against a base resolve the way a browser resolves it.
HttpURLConnection connection = (HttpURLConnection) new URL("https://example.com").openConnection();
connection.setRequestProperty("Accept", "application/json");
try (InputStream body = connection.getInputStream()) {
System.out.println(new String(body.readAllBytes()));
}
What is not here, because the platform has nothing to back it: registering a stream handler
factory, and the getContent family, which resolves a MIME type to a Java object through a
content handler. A program reaching for one fails to compile, which is where it should fail.
- Since:
- 1.0.0
-
Constructor Summary
ConstructorsConstructorDescriptionParses a URL against a base, so that a relative one resolves.Parses a URL.Builds a URL from its parts.Builds a URL from its parts. -
Method Summary
Modifier and TypeMethodDescriptionbooleanReturns the host and port as written, which is the host alone on a default port.intReturns the port the scheme uses when none is written, or -1 for a scheme with none.getFile()Returns the path with any query, which is what the request line carries.getHost()Returns the host, without the port.getPath()Returns the path.intgetPort()Returns the port, or -1 when the URL leaves the scheme's own port implied.Returns the scheme, without the colon.getQuery()Returns the query, without the question mark, or null when there is none.getRef()Returns the fragment, without the hash, or null when there is none.Returns the user information before the host, or null when there is none.inthashCode()Opens a connection to this URL, without sending anything yet.Sends a GET and returns the body.booleanWhether two URLs name the same thing apart from the fragment.Returns the URL as text.toString()toURI()This URL as aURI.
-
Constructor Details
-
URL
Parses a URL.- Parameters:
spec- the URL- Throws:
MalformedURLException- when the platform cannot parse it
-
URL
Parses a URL against a base, so that a relative one resolves.- Parameters:
context- the base, or nullspec- the URL, absolute or relative- Throws:
MalformedURLException- when the platform cannot parse it
-
URL
Builds a URL from its parts.- Parameters:
protocol- the scheme, without the colonhost- the hostfile- the path, with any query- Throws:
MalformedURLException- when the parts do not make a URL
-
URL
Builds a URL from its parts.- Parameters:
protocol- the scheme, without the colonhost- the hostport- the port, or -1 for the scheme's ownfile- the path, with any query- Throws:
MalformedURLException- when the parts do not make a URL
-
-
Method Details
-
getProtocol
Returns the scheme, without the colon.- Returns:
- the scheme, without the colon
-
getHost
Returns the host, without the port.- Returns:
- the host, without the port
-
getAuthority
Returns the host and port as written, which is the host alone on a default port.- Returns:
- the host and port as written, which is the host alone on a default port
-
getPort
public int getPort()Returns the port, or -1 when the URL leaves the scheme's own port implied.- Returns:
- the port, or -1 when the URL leaves the scheme's own port implied
-
getDefaultPort
public int getDefaultPort()Returns the port the scheme uses when none is written, or -1 for a scheme with none.- Returns:
- the port the scheme uses when none is written, or -1 for a scheme with none
-
getPath
Returns the path.- Returns:
- the path
-
getQuery
Returns the query, without the question mark, or null when there is none.- Returns:
- the query, without the question mark, or null when there is none
-
getFile
Returns the path with any query, which is what the request line carries.- Returns:
- the path with any query, which is what the request line carries
-
getRef
Returns the fragment, without the hash, or null when there is none.- Returns:
- the fragment, without the hash, or null when there is none
-
getUserInfo
Returns the user information before the host, or null when there is none.- Returns:
- the user information before the host, or null when there is none
-
openConnection
Opens a connection to this URL, without sending anything yet.- Returns:
- the connection, an
HttpURLConnectionfor an HTTP or HTTPS URL - Throws:
IOException- when the scheme is one the platform cannot reach
-
openStream
Sends a GET and returns the body.- Returns:
- the body
- Throws:
IOException- when the request fails
-
toURI
This URL as aURI.- Returns:
- the URI
- Throws:
URISyntaxException- when the URI parser is stricter about it than the URL parser was
-
toExternalForm
Returns the URL as text.- Returns:
- the URL as text
-
sameFile
Whether two URLs name the same thing apart from the fragment.- Parameters:
other- the other URL- Returns:
- whether they do
-
equals
-
hashCode
public int hashCode() -
toString
-