Java - URLConnection setContentHandlerFactory(ContentHandlerFactory fac)
Description
The Java URLConnection setContentHandlerFactory(ContentHandlerFactory fac) method sets the ContentHandlerFactory of an application. It can be called at most once by an application. The ContentHandlerFactory instance is used to construct a content handler from a content type. If there is a security manager, this method first calls the security manager's checkSetFactory method to ensure the operation is allowed. This could result in a SecurityException.
Declaration
Following is the declaration for java.net.URLConnection.setContentHandlerFactory(ContentHandlerFactory fac) method
public static void setContentHandlerFactory(ContentHandlerFactory fac)
Parameters
fac − the desired factory.
Return Value
NA
Exception
Error − if the application has already set a factory.
SecurityException − if a security manager exists and its checkSetFactory method doesn't allow the operation.
Example 1
The following example shows the usage of Java URLConnection setContentHandlerFactory() 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();
} 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