Java - URLConnection setAllowUserInteraction(boolean allowuserinteraction)
Description
The Java URLConnection setAllowUserInteraction(boolean allowuserinteraction) method sets the value of the allowUserInteraction field for this object.
Declaration
Following is the declaration for java.net.URLConnection.setAllowUserInteraction(boolean allowuserinteraction) method
public void setAllowUserInteraction(boolean allowuserinteraction)
Parameters
allowuserinteraction − the new value.
Return Value
NA
Exception
IllegalStateException − if already connected
Example 1
The following example shows the usage of Java URLConnection setAllowUserInteraction() 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 getAllowUserInteraction(), we're getting the status of allowUserInteraction field of URLConnection instance and printing the same. Now using setAllowUserInteraction() method, we're setting the value of allowUserInteraction field as true and then using getAllowUserInteraction(), we're getting the status of allowUserInteraction field 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.getAllowUserInteraction());
urlConnection.setAllowUserInteraction(true);
System.out.println(urlConnection.getAllowUserInteraction());
} catch (IOException e) {
e.printStackTrace();
}
}
}
Let us compile and run the above program, this will produce the following result −
Output
false true
Example 2
The following example shows the usage of Java URLConnection setAllowUserInteraction() 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 setAllowUserInteraction() method, we're setting the value of allowUserInteraction field as true and then using getAllowUserInteraction(), we're getting the status of allowUserInteraction field 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.setAllowUserInteraction(true);
System.out.println(urlConnection.getAllowUserInteraction());
} catch (IOException e) {
e.printStackTrace();
}
}
}
Let us compile and run the above program, this will produce the following result −
Output
true
Example 3
The following example shows the usage of Java URLConnection setAllowUserInteraction() 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 setAllowUserInteraction() method, we're setting the value of allowUserInteraction field as false and then using getAllowUserInteraction(), we're getting the status of allowUserInteraction field 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.setAllowUserInteraction(false);
System.out.println(urlConnection.getAllowUserInteraction());
} catch (IOException e) {
e.printStackTrace();
}
}
}
Let us compile and run the above program, this will produce the following result −
Output
false
java_urlconnection.htm