Java Tutorial

Java Control Statements

Object Oriented Programming

Java Built-in Classes

Java File Handling

Java Error & Exceptions

Java Multithreading

Java Synchronization

Java Networking

Java Collections

Java List Interface

Java Queue Interface

Java Map Interface

Java Set Interface

Java Data Structures

Java Collections Algorithms

Advanced Java

Java Miscellaneous

Java APIs & Frameworks

Java Useful Resources

Java - URLConnection setReadTimeout(int timeout)



Description

The Java URLConnection setReadTimeout(int timeout) method sets the read timeout to a specified timeout, in milliseconds. A non-zero value specifies the timeout when reading from Input stream when a connection is established to a resource. If the timeout expires before there is data available for read, a java.net.SocketTimeoutException is raised. A timeout of zero is interpreted as an infinite timeout.

Declaration

Following is the declaration for java.net.URLConnection.setReadTimeout(int timeout) method

public void setReadTimeout(int timeout)

Parameters

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

Return Value

an int that indicates the read timeout value in milliseconds

Exception

IllegalArgumentException − if the timeout parameter is negative.

Example 1

The following example shows the usage of Java URLConnection setReadTimeout() method for a valid url with https protocol. In this example, we're creating an instance of URL class. Using url.openConnection() method, we're getting the URLConnection instance. Using getReadTimeout(), we're getting the read timeout of URLConnection instance and printing the same. Then using setReadTimeout() method we've increased the value of read timeout by 1 hour to true and printed the same again −

package com.tutorialspoint;

import java.io.IOException;
import java.net.URL;
import java.net.URLConnection;
import java.time.Instant;
import java.util.Date;

public class UrlConnectionDemo {
   public static void main(String [] args) {
      try {
         URL url = new URL("https://www.tutorialspoint.com");
         URLConnection urlConnection = url.openConnection();
         int headerValue = urlConnection.getReadTimeout();
         System.out.println("Read Timeout: " + Date.from(Instant.ofEpochMilli(headerValue)));
         headerValue += 3600000;
         urlConnection.setReadTimeout(headerValue);
         System.out.println("Updated Read Timeout: " + Date.from(Instant.ofEpochMilli(headerValue)));
      } catch (IOException e) {
         e.printStackTrace();
      }
   }
}

Let us compile and run the above program, this will produce the following result −

Output

Read Timeout: Thu Jan 01 05:30:00 IST 1970
Updated Read Timeout: Thu Jan 01 06:30:00 IST 1970

Example 2

The following example shows the usage of Java URLConnection setReadTimeout() method for a valid url with http protocol. In this example, we're creating an instance of URL class. Using url.openConnection() method, we're getting the URLConnection instance. Using getReadTimeout(), we're getting the read timeout of URLConnection instance and printing the same. Then using setReadTimeout() method we've increased the value of read timeout by 1 hour and printed the same again −

package com.tutorialspoint;

import java.io.IOException;
import java.net.URL;
import java.net.URLConnection;
import java.time.Instant;
import java.util.Date;

public class UrlConnectionDemo {
   public static void main(String [] args) {
      try {
         URL url = new URL("http://www.tutorialspoint.com");
         URLConnection urlConnection = url.openConnection();
         int headerValue = urlConnection.getReadTimeout();
         System.out.println("Read Timeout: " + Date.from(Instant.ofEpochMilli(headerValue)));
         headerValue += 3600000;
         urlConnection.setReadTimeout(headerValue);
         System.out.println("Updated Read Timeout: " + Date.from(Instant.ofEpochMilli(headerValue)));
      } catch (IOException e) {
         e.printStackTrace();
      }
   }
}

Let us compile and run the above program, this will produce the following result −

Output

Read Timeout: Thu Jan 01 05:30:00 IST 1970
Updated Read Timeout: Thu Jan 01 06:30:00 IST 1970

Example 3

The following example shows the usage of Java URLConnection setReadTimeout() method for a valid url with http protocol. In this example, we're creating an instance of URL class. Using url.openConnection() method, we're getting the URLConnection instance. Using getReadTimeout(), we're getting the read timeout of URLConnection instance and printing the same. Then using setReadTimeout() method we've increased the value of read timeout by 1 hour and printed the same again −

package com.tutorialspoint;

import java.io.IOException;
import java.net.URL;
import java.net.URLConnection;
import java.time.Instant;
import java.util.Date;

public class UrlConnectionDemo {
   public static void main(String [] args) {
      try {
         URL url = new URL("http://www.google.com");
         URLConnection urlConnection = url.openConnection();
         int headerValue = urlConnection.getReadTimeout();
         System.out.println("Read Timeout: " + Date.from(Instant.ofEpochMilli(headerValue)));
         headerValue += 3600000;
         urlConnection.setReadTimeout(headerValue);
         System.out.println("Updated Read Timeout: " + Date.from(Instant.ofEpochMilli(headerValue)));
      } catch (IOException e) {
         e.printStackTrace();
      }
   }
}

Let us compile and run the above program, this will produce the following result −

Output

Read Timeout: Thu Jan 01 05:30:00 IST 1970
Updated Read Timeout: Thu Jan 01 06:30:00 IST 1970
java_urlconnection.htm
Advertisements