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



The Java HttpURLConnection getPermission() method returns a SocketPermission object representing the permission necessary to connect to the destination host and port. It overrides getPermission() method of URLConnection class.

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

public Permission getPermission() throws IOException

Parameters

NA

Return Value

a SocketPermission object representing the permission necessary to connect to the destination host and port.

Exception

IOException − if an error occurs while computing the permission.

Example 1

The following example shows the usage of Java HttpURLConnection 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 HttpURLConnection 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.HttpURLConnection;
import java.security.Permission;

public class HttpURLConnectionDemo {
   public static void main(String [] args) {
      try {
         URL url = new URL("https://www.tutorialspoint.com");
         HttpURLConnection httpURLConnection = (HttpURLConnection)url.openConnection();
         Permission permission = httpURLConnection.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 HttpURLConnection 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 HttpURLConnection 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.HttpURLConnection;
import java.security.Permission;

public class HttpUrlConnectionDemo {
   public static void main(String [] args) {
      try {
         URL url = new URL("http://www.tutorialspoint.com");
         HttpURLConnection httpURLConnection = (HttpURLConnection)url.openConnection();
         Permission permission = httpURLConnection.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 HttpURLConnection 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 HttpURLConnection 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.HttpURLConnection;
import java.security.Permission;

public class HttpUrlConnectionDemo {
   public static void main(String [] args) {
      try {
         URL url = new URL("http://www.google.com");
         HttpURLConnection httpURLConnection = (HttpURLConnection)url.openConnection();
         Permission permission = httpURLConnection.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_httpurlconnection.htm
Advertisements