URLConnection getContent() in Java

getContent(): This method is available in the java.net.URLConnection class of Java.

Syntax:

Object java.net.URLConnection.getContent() throws IOException

This method retrieves the contents of this URL connection.

This method first determines the content type of the object by calling the getContentType method. If this is the first time that the application has seen that specific content type, a content handler for that content type is created.

This is done as follows:

1. If the application has set up a content handler factory instance using the setContentHandlerFactory method, the createContentHandler method of that instance is called with the content type as an argument; the result is a content handler for that content type.

2. If no ContentHandlerFactory has yet to be set up, or if the factory's createContentHandler method returns null, then the ServiceLoader mechanism is used to locate ContentHandlerFactoryimplementations using the system classloader. The order that factories are located is implementation specific, and an implementation is free to cache the located factories. A  ServiceConfigurationError, Error, or RuntimeException thrown from the createContentHandler, if encountered, will be propagated to the calling thread. The createContentHandler method of each factory, if instantiated, is invoked, with the content type, until a factory returns non-null, or all factories have been exhausted.

3. Failing that, this method tries to load a content handler class as defined by ContentHandler.If the class does not exist or is not a subclass of ContentHandler, then an UnknownServiceException is thrown.

Parameters: NA

Returns: the object fetched. The instanceof operator should be used to determine the specific kind of object returned.

Throws:

1. IOException - if an I/O error occurs while getting the content.

2. UnknownServiceException - if the protocol does not support the content type.

Approach

Java

package com.URLConnection;

import java.io.IOException;
import java.net.URL;
import java.net.URLConnection;

public class URLConnectiongetContent {
    public static void main(String[] args) throws IOException {

        URL url = new URL("https://beingcodeexpert.blogspot.com/");
        URLConnection urlConnection = url.openConnection();

        System.out.println(urlConnection.getContent());
    }
}

Output:

sun.net.www.protocol.http.HttpURLConnection$HttpInputStream@3ee37e5a


Some other methods of URLConnection class

addRequestProperty(String, String)This method adds a general request property specified by a key-value pair.

connect()This method opens a communications link to the resource referenced by this URL if such a connection has not already been established.

getAllowUserInteraction()This method returns the value of the allowUserInteraction field for this object.

getConnectTimeout()This method returns the setting for connect timeout.

getContent()This method retrieves the contents of this URL connection.

getContent(Class<?>[]) This method retrieves the contents of this URL connection.

getContentEncoding()This method returns the value of the content-encoding header field.

getContentLength()This method returns the value of the content-length header field.

getContentLengthLong()This method returns the value of the content-length header field as long.

getContentType()This method returns the value of the content-type header field.

getDate()This method returns the value of the Date header field.

URLConnection.getDefaultAllowUserInteraction()This method returns the default value of the allowUserInteractionfield.

getDefaultUseCaches()This method returns the default value of a URLConnection's useCaches flag.

URLConnection.getDefaultUseCaches(String)This method returns the default value of the useCaches flag for the given protocol.

getDoInput()This method returns the value of this URLConnection's input flag.

getDoOutput()This method returns the value of this URLConnection's output flag.

getExpiration()This method returns the value of the expires header field.

URLConnection.getFileNameMap()This method loads a filename map (a mimetable) from a data file.

getHeaderField(int)This method returns the value for the nth header field. It returns null if there are fewer than n+1 fields.

getHeaderField(String)This method returns the value of the named header field.

getHeaderFieldDate(String, long)This method returns the value of the named field parsed as a date.

getHeaderFieldInt(String, int)This method returns the value of the named field parsed as a number.

getHeaderFieldKey(int)This method returns the key for the nth header field. 

getHeaderFieldLong(String, long)This method returns the value of the named field parsed as a number.

getHeaderFields()This method returns an unmodifiable Map of the header fields.

getIfModifiedSince()This method returns the value of this object's ifModifiedSince field.

getInputStream()This method returns an input stream that reads from this open connection.

getLastModified()This method returns the value of the last-modified header field. 

getOutputStream()This method returns an output stream that writes to this connection.

getPermission()This method returns a permission object representing the permission necessary to make the connection represented by this object.

getReadTimeout()This method returns a setting for read timeout. 0 return implies that the option is disabled (i.e., timeout of infinity).

getRequestProperties()This method returns an unmodifiable Map of general request properties for this connection.

getRequestProperty(String)This method returns the value of the named general request property for this connection.

getURL()This method returns the value of this URLConnection's URL field.

getUseCaches()This method returns the value of this URLConnection's useCaches field.

URLConnection.guessContentTypeFromName(String)This method tries to determine the content type of an object, based on the specified  "file" component of a URL.

URLConnection.guessContentTypeFromStream(InputStream)This method tries to determine the type of input stream based on the characters at the beginning of the input stream.

setAllowUserInteraction(boolean)This method set the value of the allowUserInteraction field of this URLConnection.

setConnectTimeout(int)This method sets a specified timeout value, in milliseconds, to be used when opening a communications link to the resource referenced by this URLConnection.

URLConnection.setContentHandlerFactory(ContentHandlerFactory)This method sets the ContentHandlerFactory of an application. It can be called at most once by an application.

setDefaultUseCaches(boolean)This method sets the default value of the useCaches field to the specified value.

setDefaultUseCaches(String, boolean)This method sets the default value of the useCaches field for the named protocol to the given value.

setDoInput(boolean)This method sets the value of the doInput field for this URLConnection to the specified value.

setDoOutput(boolean)This method sets the value of the doOutput field for this URLConnection to the specified value.

URLConnection.setFileNameMap(FileNameMap):  This method sets the FileNameMap.

setIfModifiedSince(long)This method sets the value of the ifModifiedSince field of this URLConnection to the specified value.

setReadTimeout(int)This method sets the read timeout to a specified timeout, in milliseconds.

setRequestProperty(String, String)This method sets the general request property. If a property with the key already exists, overwrite its value with the new value.

setUseCaches(boolean)This method sets the value of the useCaches field of this URLConnection to the specified value.

toString()This method returns a String representation of this URL connection.

No comments:

Post a Comment