Java Tutorial

Java Control Statements

Object Oriented Programming

Java Built-in Classes

Java File Handling

Java Error & Exceptions

Java Multithreading

Java Synchronization

Java Networking

Java Collections

Java List Interface

Java Queue Interface

Java Map Interface

Java Set Interface

Java Data Structures

Java Collections Algorithms

Advanced Java

Java Miscellaneous

Java APIs & Frameworks

Java Useful Resources

Java - URLConnection setDefaultAllowUserInteraction(boolean defaultallowuserinteraction)



Description

The Java URLConnection setDefaultAllowUserInteraction(boolean defaultallowuserinteraction) method sets the default value of the allowUserInteraction field for all future URLConnection objects to the specified value.

Declaration

Following is the declaration for java.net.URLConnection.setDefaultAllowUserInteraction(boolean defaultallowuserinteraction) method

public static void setDefaultAllowUserInteraction(boolean defaultallowuserinteraction)

Parameters

defaultallowuserinteraction − the new value.

Return Value

the default value of the allowUserInteraction field.

Exception

NA

Example 1

The following example shows the usage of Java URLConnection setDefaultAllowUserInteraction() 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 getDefaultAllowUserInteraction(), we're getting the default value of the allowUserInteraction field of URLConnection instance and printing the same. Now using setDefaultAllowUserInteraction() we're setting the default value of the allowUserInteraction field of URLConnection instance as true and then 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.getDefaultAllowUserInteraction());
         urlConnection.setDefaultAllowUserInteraction(true);
         System.out.println(urlConnection.getDefaultAllowUserInteraction());
      } 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 setDefaultAllowUserInteraction() 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 getDefaultAllowUserInteraction(), we're getting the default value of the allowUserInteraction field of URLConnection instance and printing the same.Now using setDefaultAllowUserInteraction() we're setting the default value of the allowUserInteraction field of URLConnection instance as true and then 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("http://www.tutorialspoint.com");
         URLConnection urlConnection = url.openConnection();

         System.out.println(urlConnection.getDefaultAllowUserInteraction());
         urlConnection.setDefaultAllowUserInteraction(true);
         System.out.println(urlConnection.getDefaultAllowUserInteraction());
      } catch (IOException e) {
         e.printStackTrace();
      }
   }
}

Let us compile and run the above program, this will produce the following result −

Output

false
true

Example 3

The following example shows the usage of Java URLConnection setDefaultAllowUserInteraction() method for a url of google 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 getDefaultAllowUserInteraction(), we're getting the default value of the allowUserInteraction field of URLConnection instance and printing the same.Now using setDefaultAllowUserInteraction() we're setting the default value of the allowUserInteraction field of URLConnection instance as true and then 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("http://www.google.com");
         URLConnection urlConnection = url.openConnection();

         System.out.println(urlConnection.getDefaultAllowUserInteraction());
         urlConnection.setDefaultAllowUserInteraction(true);
         System.out.println(urlConnection.getDefaultAllowUserInteraction());
      } catch (IOException e) {
         e.printStackTrace();
      }
   }
}

Let us compile and run the above program, this will produce the following result −

Output

false
true
java_urlconnection.htm
Advertisements