Java - URLConnection setConnectTimeout()
Description
The Java URLConnection setConnectTimeout() method sets a specified timeout value, in milliseconds, to be used when opening a communications link to the resource referenced by this URLConnection. If the timeout expires before the connection can be established, 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.setConnectTimeout() method
public void setConnectTimeout(int timeout)
Parameters
timeout − an int that specifies the connect timeout value in milliseconds
Return Value
NA
Exception
IllegalArgumentException − if the timeout parameter is negative
Example 1
The following example shows the usage of Java URLConnection setConnectTimeout() 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 getConnectTimeout(), we're getting the value of connect timeout of URLConnection instance and printing the same. Now using setConnectTimeout() method, we're setting the value of connect timeout as 100 and then using getConnectTimeout(), we're getting the value of connect timeout of URLConnection instance and printing the same −
package com.tutorialspoint;
import java.io.IOException;
import java.net.URL;
import java.net.URLConnection;
public class UrlConnectionDemo {
public static void main(String [] args) {
try {
URL url = new URL("https://www.tutorialspoint.com");
URLConnection urlConnection = url.openConnection();
System.out.println(urlConnection.getConnectTimeout());
urlConnection.setConnectTimeout(100);
System.out.println(urlConnection.getConnectTimeout());
} catch (IOException e) {
e.printStackTrace();
}
}
}
Let us compile and run the above program, this will produce the following result −
Output
0 100
Example 2
The following example shows the usage of Java URLConnection setConnectTimeout() 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.Now using setConnectTimeout() method, we're setting the value of connect timeout as 100 and then using getConnectTimeout(), we're getting the value of connect timeout of URLConnection instance and printing the same −
package com.tutorialspoint;
import java.io.IOException;
import java.net.URL;
import java.net.URLConnection;
public class UrlConnectionDemo {
public static void main(String [] args) {
try {
URL url = new URL("https://www.tutorialspoint.com");
URLConnection urlConnection = url.openConnection();
urlConnection.setConnectTimeout(100);
System.out.println(urlConnection.getConnectTimeout());
} catch (IOException e) {
e.printStackTrace();
}
}
}
Let us compile and run the above program, this will produce the following result −
Output
100
Example 3
The following example shows the usage of Java URLConnection setConnectTimeout() 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.Now using setConnectTimeout() method, we're setting the value of connect timeout as 0 to disable it and then using getConnectTimeout(), we're getting the value of connect timeout of URLConnection instance and printing the same −
package com.tutorialspoint;
import java.io.IOException;
import java.net.URL;
import java.net.URLConnection;
public class UrlConnectionDemo {
public static void main(String [] args) {
try {
URL url = new URL("https://www.tutorialspoint.com");
URLConnection urlConnection = url.openConnection();
urlConnection.setConnectTimeout(0);
System.out.println(urlConnection.getConnectTimeout());
} catch (IOException e) {
e.printStackTrace();
}
}
}
Let us compile and run the above program, this will produce the following result −
Output
0
java_urlconnection.htm