Java - URLConnection getReadTimeout() Method
Description
The Java URLConnection getReadTimeout() method returns setting for read timeout. 0 return implies that the option is disabled (i.e., timeout of infinity).
Declaration
Following is the declaration for java.net.URLConnection.getReadTimeout() method
public int getReadTimeout()
Parameters
NA
Return Value
an int that indicates the read timeout value in milliseconds
Exception
NA
Example 1
The following example shows the usage of Java URLConnection getReadTimeout() 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 −
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();
long headerValue = urlConnection.getReadTimeout();
System.out.println("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
Example 2
The following example shows the usage of Java URLConnection getReadTimeout() 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 −
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();
long headerValue = urlConnection.getReadTimeout();
System.out.println("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
Example 3
The following example shows the usage of Java URLConnection getReadTimeout() 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 −
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();
long headerValue = urlConnection.getReadTimeout();
System.out.println("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
java_urlconnection.htm