getContent(): This method is available in the java.net.URL class of Java.
Syntax:
Object java.net.URL.getContent() throws IOException
This method gets the contents of this URL.
Parameters: NA
Returns: the contents of this URL.
Throws:
1. IOException - if an I/O exception occurs.
Approach
Java
package com.URL;import java.io.IOException;import java.net.URL;public class URLgetContent {public static void main(String[] args) throws IOException {String protocol = "http", host = "localhost", file = "/hello";int port = 8099;URL url = new URL(protocol, host, port, file);System.out.println(url.getContent());}}
Output:
Exception in thread "main" java.net.ConnectException: Connection refused: connect at java.base/sun.nio.ch.Net.connect0(Native Method) at java.base/sun.nio.ch.Net.connect(Net.java:574) at java.base/sun.nio.ch.Net.connect(Net.java:563) at java.base/sun.nio.ch.NioSocketImpl.connect(NioSocketImpl.java:588) at java.base/java.net.Socket.connect(Socket.java:648) at java.base/java.net.Socket.connect(Socket.java:597) at java.base/sun.net.NetworkClient.doConnect(NetworkClient.java:182) at java.base/sun.net.www.http.HttpClient.openServer(HttpClient.java:474) at java.base/sun.net.www.http.HttpClient.openServer(HttpClient.java:569) at java.base/sun.net.www.http.HttpClient.<init>(HttpClient.java:242) at java.base/sun.net.www.http.HttpClient.New(HttpClient.java:341) at java.base/sun.net.www.http.HttpClient.New(HttpClient.java:362) at java.base/sun.net.www.protocol.http.HttpURLConnection.getNewHttpClient(HttpURLConnection.java:1261) at java.base/sun.net.www.protocol.http.HttpURLConnection.plainConnect0(HttpURLConnection.java:1194) at java.base/sun.net.www.protocol.http.HttpURLConnection.plainConnect(HttpURLConnection.java:1082) at java.base/sun.net.www.protocol.http.HttpURLConnection.connect(HttpURLConnection.java:1016) at java.base/sun.net.www.protocol.http.HttpURLConnection.getInputStream0(HttpURLConnection.java:1600) at java.base/sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1528) at java.base/java.net.URLConnection.getContent(URLConnection.java:752) at java.base/java.net.URL.getContent(URL.java:1181) at com.URL.URLgetContent.main(URLgetContent.java:13)
Some other methods of the URL class
URL(String): This method creates a URL object from the String representation.
URL(URL, String): This method creates a URL by parsing the given spec within a specified context.
URL(String, String, String): This method creates a URL from the specified protocol name, hostname, and file name.
URL(URL, String, URLStreamHandler): This method creates a URL by parsing the given spec with the specified handler within a specified context.
URL(String, String, int, String): This method creates a URL object from the specified protocol, host, port number, and file. host can be expressed as a hostname or a literal IP address.
URL(String, String, int, String, URLStreamHandler): This method creates a URL object from the specified protocol, host, port number, file, and handler.
equals(Object): This method compares this URL for equality with another object.
getAuthority(): This method gets the authority part of this URL.
getContent(): This method gets the contents of this URL.
getContent(Class<?>[]): This method gets the contents of this URL.
getDefaultPort(): This method gets the default port number of the protocol associated with this URL.
getFile(): This method gets the file name of this URL.
getHost(): This method gets the hostname of this URL, if applicable.
getPath(): This method gets the path part of this URL.
getPort(): This method gets the port number of this URL.
getProtocol(): This method gets the protocol name of this URL.
getQuery(): This method gets the query part of this URL.
getRef(): This method gets the anchor (also known as the "reference") of this URL.
getUserInfo(): This method gets the userInfo part of this URL.
hashCode(): This method creates an integer suitable for hash table indexing.
openConnection(): This method returns a URLConnection instance that represents a connection to the remote object referred to by the URL.
openConnection(Proxy): This method returns a URLConnection instance that represents a connection to the remote object referred to by the URL with Proxy.
openStream(): This method opens a connection to this URL and returns an InputStream for reading from that connection.
sameFile(URL): This method compares two URLs, excluding the fragment component.
URL.setURLStreamHandlerFactory(URLStreamHandlerFactory): This method sets an application's URLStreamHandlerFactory.This method can be called at most once in a given Java VirtualMachine.
toExternalForm(): This method constructs a string representation of this URL.
toString(): This method constructs a string representation of this URL. The string is created by calling the toExternalForm method of the stream protocol handler for this object.
toURI(): This method returns a java.net.URI equivalent to this URL.
No comments:
Post a Comment