Java - URLConnection getConnectTimeout() Method
Description
The Java URLConnection getConnectTimeout() method returns setting for connect timeout. In case of 0 value, no timeout that is this option is disabled.
Declaration
Following is the declaration for java.net.URLConnection.getConnectTimeout() method
public int getConnectTimeout()
Parameters
NA
Return Value
an int that indicates the connect timeout value in milliseconds.
Exception
NA
Example 1
The following example shows the usage of Java URLConnection getConnectTimeout() 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 −
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());
} catch (IOException e) {
e.printStackTrace();
}
}
}
Let us compile and run the above program, this will produce the following result −
Output
0
Example 2
The following example shows the usage of Java URLConnection getConnectTimeout() 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 getConnectTimeout() 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