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 getPermission() Method



Description

The Java URLConnection getPermission() method returns a permission object representing the permission necessary to make the connection represented by this object. This method returns null if no permission is required to make the connection. By default, this method returns java.security.AllPermission. Subclasses should override this method and return the permission that best represents the permission required to make a connection to the URL. For example, a URLConnection representing a file: URL would return a java.io.FilePermission object.

Following is the declaration for java.net.URLConnection.getPermission() method

public Permission getPermission() throws IOException

Parameters

NA

Return Value

the permission object representing the permission necessary to make the connection represented by this URLConnection.

Exception

IOException − if the computation of the permission requires network or file I/O and an exception occurs while computing it.

Example 1

The following example shows the usage of Java URLConnection getPermission() 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 getPermission(), we're getting the value of an Permossion object and printing the same −

package com.tutorialspoint;

import java.io.IOException;
import java.net.URL;
import java.net.URLConnection;
import java.security.Permission;

public class UrlConnectionDemo {
   public static void main(String [] args) {
      try {
         URL url = new URL("https://www.tutorialspoint.com");
         URLConnection urlConnection = url.openConnection();
         Permission permission = urlConnection.getPermission();
         System.out.println("Permission: " + permission);
      } catch (IOException e) {
         e.printStackTrace();
      }
   }
}

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

Output

Permission: ("java.net.SocketPermission" "www.tutorialspoint.com:80" "connect,resolve")

Example 2

The following example shows the usage of Java URLConnection getPermission() 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 getPermission(), we're getting the value of an Permossion object and printing the same −

package com.tutorialspoint;

import java.io.IOException;
import java.net.URL;
import java.net.URLConnection;
import java.security.Permission;

public class UrlConnectionDemo {
   public static void main(String [] args) {
      try {
         URL url = new URL("http://www.tutorialspoint.com");
         URLConnection urlConnection = url.openConnection();
         Permission permission = urlConnection.getPermission();
         System.out.println("Permission: " + permission);
      } catch (IOException e) {
         e.printStackTrace();
      }
   }
}

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

Output

Permission: ("java.net.SocketPermission" "www.tutorialspoint.com:80" "connect,resolve")

Example 3

The following example shows the usage of Java URLConnection getPermission() 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 getPermission(), we're getting the value of an Permossion object and printing the same −

package com.tutorialspoint;

import java.io.IOException;
import java.net.URL;
import java.net.URLConnection;
import java.security.Permission;

public class UrlConnectionDemo {
   public static void main(String [] args) {
      try {
         URL url = new URL("http://www.google.com");
         URLConnection urlConnection = url.openConnection();
         Permission permission = urlConnection.getPermission();
         System.out.println("Permission: " + permission);
      } catch (IOException e) {
         e.printStackTrace();
      }
   }
}

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

Output

Permission: ("java.net.SocketPermission" "www.google.com:80" "connect,resolve")
java_urlconnection.htm
Advertisements