URLConnection setReadTimeout(int) in Java

setReadTimeout(int): This method is available in the java.net.URLConnection class of java.

Syntax:

void java.net.URLConnection.setReadTimeout(int timeout)

This method takes one argument. This method sets the read timeout to a specified timeout, in milliseconds.

A non-zero value specifies the timeout when reading from the Input stream when a connection is established to our source.

If the timeout expires before there is data available for reading, a java.net.SocketTimeoutException is raised. A timeout of zero is interpreted as an infinite timeout.

Some non-standard implementation of this method ignores the specified timeout.

Parameters: One parameter is required for this method.

timeout: an int that specifies the timeout value to be used in milliseconds.

Returns: NA

Throws:

1. IllegalArgumentException - if the timeout parameter is negative.

Approach 1: When no exception

Java

package com.URLConnection;

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

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

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

        int timeout = 10000911;
        urlConnection.setReadTimeout(timeout);
        System.out.println("Successfully setReadTimeout");
    }
}

Output:

Successfully setReadTimeout


Approach 2: IllegalArgumentException

Java

package com.URLConnection;

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

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

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

        int timeout = -10000911;
        urlConnection.setReadTimeout(timeout);
        System.out.println("Successfully setReadTimeout");
    }
}

Output:

Exception in thread "main" java.lang.IllegalArgumentException: timeouts can't be negative at java.base/sun.net.www.protocol.http.HttpURLConnection.setReadTimeout(HttpURLConnection.java:3360) at java.base/sun.net.www.protocol.https.HttpsURLConnectionImpl.setReadTimeout(HttpsURLConnectionImpl.java:482) at com.URLConnection.URLConnectionsetReadTimeout.main(URLConnectionsetReadTimeout.java:14)


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