Programming Blog

This blog is about technical and programming questions and there solutions. I also cover programs that were asked in various interviews, it will help you to crack the coding round of various interviews

Monday, 8 January 2018

How To Get URL Properties in Java

The URL class provides utility methods that let you query URL objects. You can get the protocol, authority, host name, port number, path, query, filename, and reference from a URL using these accessor methods:

getProtocol() method returns the protocol identifier of the URL.
getAuthority() method returns the authority of the URL.
getHost() method returns the host name of the URL.
getPort() method returns the port number of the URL. If the port is not set, then it returns -1.
getPath() method returns the path of this URL.
getQuery() method returns the query of this URL.
getFile() method returns the filename of the URL.
getRef() method returns the reference of the URL.

                          Code



import java.net.MalformedURLException;import java.net.URL;
public class MyUrlProperties {
    public static void main(String a[]){    
        try {            String url =        "http://www.m4513.blogspot.com";            URL myUrl = new URL(url);            System.out.println("Protocol: "+myUrl.getProtocol());            System.out.println("Host: "+myUrl.getHost());            System.out.println("Port: "+myUrl.getPort());            System.out.println("Athority of the URL: "+myUrl.getAuthority());            System.out.println("Query: "+myUrl.getQuery());            System.out.println("Reference: "+myUrl.getRef());        } catch (MalformedURLException ex) {            ex.printStackTrace();        }    }}


No comments:

Post a Comment