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 - URL setURLStreamHandlerFactory (URLStreamHandlerFactory)



Description

The Java URL setURLStreamHandlerFactory(URLStreamHandlerFactory) method sets an application's URLStreamHandlerFactory. This method can be called at most once in a given Java Virtual Machine. The URLStreamHandlerFactory instance is used to construct a stream protocol handler from a protocol name. 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.URL.setURLStreamHandlerFactory(URLStreamHandlerFactory) method

public static void setURLStreamHandlerFactory(URLStreamHandlerFactory 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 setURLStreamHandlerFactory() 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 setURLStreamHandlerFactory() method, we're setting null as contentHandlerFactory to see the impact and then using getInputStream(), we're getting the content of the home page of the website and printing the same −

package com.tutorialspoint;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
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/index.htm?language=en#j2se");
         URLConnection.setContentHandlerFactory(null);
         URLConnection urlConnection = url.openConnection();

         BufferedReader in = new BufferedReader(
            new InputStreamReader(urlConnection.getInputStream()));
         String content = "";
         String current;
         while((current = in.readLine()) != null) {
            content += current;
         }
         System.out.println(content);
      } catch (IOException e) {
         e.printStackTrace();
      }
   }
}

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

Output

<!DOCTYPE html><html lang="en"><head>	<title>Online Tutorials, Courses, and eBooks ....</body></html>

Example 2

The following example shows the usage of Java URLConnection setURLStreamHandlerFactory() 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 setURLStreamHandlerFactory() method, we're setting null as contentHandlerFactory to see the impact and then using getInputStream(), we're getting the content of the home page of the website and printing the same −

package com.tutorialspoint;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
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/index.htm?language=en#j2se");

         URLConnection urlConnection = url.openConnection();
         URLConnection.setContentHandlerFactory(null);
         BufferedReader in = new BufferedReader(
            new InputStreamReader(urlConnection.getInputStream()));
         String content = "";
         String current;
         while((current = in.readLine()) != null) {
            content += current;
         }
         System.out.println(content);
      } catch (IOException e) {
         e.printStackTrace();
      }
   }
}

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

Output

<!DOCTYPE html><html lang="en"><head>	<title>Online Tutorials, Courses, and eBooks ....</body></html>

Example 3

The following example shows the usage of Java URLConnection setURLStreamHandlerFactory() method for a valid 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 setURLStreamHandlerFactory() method, we're setting null as contentHandlerFactory to see the impact and then using getInputStream(), we're getting the content of the home page of the website and printing the same −

package com.tutorialspoint;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
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.setContentHandlerFactory(null);
         URLConnection urlConnection = url.openConnection();

         BufferedReader in = new BufferedReader(
            new InputStreamReader(urlConnection.getInputStream()));
         String content = "";
         String current;
         while((current = in.readLine()) != null) {
            content += current;
         }
         System.out.println(content);
      } catch (IOException e) {
         e.printStackTrace();
      }
   }
}

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

Output

<!doctype html><html itemscope="" itemtype="http://schema.org/WebPage" ....</body></html>
java_urlconnection.htm
Advertisements