URL URL(String, String, int, String, URLStreamHandler) in Java

URL(String, String, int, String, URLStreamHandler): This method is available in the java.net.URL class of Java.

Syntax:

java.net.URL.URL(String protocol, String host, int port, String file, URLStreamHandler handler) throws MalformedURLException

This method takes five arguments. This method creates a URL object from the specified protocol, host, port number, file, and handler.

Specifying a port number of -1 indicates that the URL should use the default port for the protocol. Specifying a handler of null indicates that the URL should use a default stream handler for the protocol.

Parameters: Five parameters are required for this method.

protocol: the name of the protocol to use.

host: the name of the host.

port: the port number on the host.file the file on the host.

handler: the stream handler for the URL.

Throws:

1. MalformedURLException - if an unknown protocol or the port is a negative number other than -1.

2. SecurityException - if a security manager exists and its checkPermission method doesn't allow specifying a stream handler explicitly.

Approach 1: When no exception

Java

package com.URL;

import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLStreamHandler;

public class URL6 {
    public static void main(String[] args) throws MalformedURLException {

        String protocol = "http", host = "localhost", file = "/hello";
        int port = 8089;

        URLStreamHandler handler = new URLStreamHandler() {

            @Override
            protected URLConnection openConnection(URL u) throws IOException {
                URL url = new URL("http://googel.com/");
                URLConnection urlcon = url.openConnection();
                return urlcon;
            }
        };
        URL url = new URL(protocol, host, port, file, handler);

        System.out.println(url);
    }
}

Output:

http://localhost:8089/hello


Approach 2: MalformedURLException

Java

package com.URL;

import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLStreamHandler;

public class URL6 {
    public static void main(String[] args) throws MalformedURLException {

        String protocol = "http", host = "localhost", file = "/hello";
        int port = -8089;

        URLStreamHandler handler = new URLStreamHandler() {

            @Override
            protected URLConnection openConnection(URL u) throws IOException {
                URL url = new URL("http://googel.com/");
                URLConnection urlcon = url.openConnection();
                return urlcon;
            }
        };
        URL url = new URL(protocol, host, port, file, handler);

        System.out.println(url);
    }
}

Output:

Exception in thread "main" java.net.MalformedURLException: Invalid port number :-8089 at java.base/java.net.URL.<init>(URL.java:455) at com.URL.URL6.main(URL6.java:24)


Some other methods of the URL class

URL(String)This method creates a URL object from the String representation.

URL(URL, String)This method creates a URL by parsing the given spec within a specified context.

URL(String, String, String)This method creates a URL from the specified protocol name, hostname, and file name.

URL(URL, String, URLStreamHandler)This method creates a URL by parsing the given spec with the specified handler within a specified context.

URL(String, String, int, String)This method creates a URL object from the specified protocol, host, port number, and file. host can be expressed as a hostname or a literal IP address.

URL(String, String, int, String, URLStreamHandler)This method creates a URL object from the specified protocol, host, port number, file, and handler.

equals(Object)This method compares this URL for equality with another object.

getAuthority()This method gets the authority part of this URL.

getContent()This method gets the contents of this URL.

getContent(Class<?>[])This method gets the contents of this URL.

getDefaultPort()This method gets the default port number of the protocol associated with this URL.

getFile()This method gets the file name of this URL. 

getHost()This method gets the hostname of this URL, if applicable.

getPath()This method gets the path part of this URL.

getPort()This method gets the port number of this URL.

getProtocol()This method gets the protocol name of this URL.

getQuery()This method gets the query part of this URL.

getRef()This method gets the anchor (also known as the "reference") of this URL.

getUserInfo()This method gets the userInfo part of this URL.

hashCode()This method creates an integer suitable for hash table indexing.

openConnection()This method returns a URLConnection instance that represents a connection to the remote object referred to by the URL.

openConnection(Proxy)This method returns a URLConnection instance that represents a connection to the remote object referred to by the URL with Proxy.

openStream()This method opens a connection to this URL and returns an InputStream for reading from that connection.

sameFile(URL)This method compares two URLs, excluding the fragment component.

URL.setURLStreamHandlerFactory(URLStreamHandlerFactory)This method sets an application's URLStreamHandlerFactory.This method can be called at most once in a given Java VirtualMachine.

toExternalForm()This method constructs a string representation of this URL. 

toString()This method constructs a string representation of this URL. The string is created by calling the toExternalForm method of the stream protocol handler for this object.

toURI()This method returns a java.net.URI equivalent to this URL.

No comments:

Post a Comment