URL class provides number of getter methods to get the various portions of the URL.
String getAuthority()
Gets the authority part of this URL.
int getDefaultPort()
Gets the default port number of the protocol associated with this URL.
String getFile()
Gets the file name of this URL. “getFile()” method returns path including query string.
String getHost()
Gets the host name of this URL, if applicable.
String getPath()
Gets the path part of this URL. “getPath()” method doesn’t return query string.
int getPort()
Gets the port number of this URL. If no port specified in the URL, then it returns -1.
String getProtocol()
Gets the protocol name of this URL.
String getQuery()
Gets the query part of this URL.
String getRef()
Gets the anchor (also known as the "reference") of this URL.
String getUserInfo()
Gets the userInfo part of this URL.
Gets the userInfo part of this URL.
import java.io.IOException; import java.net.URL; public class Main { public static void main(String args[]) throws IOException { String location = "https://docs.oracle.com/javase/7/docs/api/java/net/URL.html#getContent()"; URL url = new URL(location); System.out.println("Authority : " +url.getAuthority()); System.out.println("Deafult Port : " +url.getDefaultPort()); System.out.println("File : " +url.getFile()); System.out.println("Host : " +url.getHost()); System.out.println("Path : " +url.getPath()); System.out.println("Port : " +url.getPort()); System.out.println("Protocol : " +url.getProtocol()); System.out.println("Query : " +url.getQuery()); System.out.println("Reference (Anchor) : " +url.getRef()); System.out.println("User Info : " +url.getUserInfo()); } }
Output
Authority : docs.oracle.com Deafult Port : 443 File : /javase/7/docs/api/java/net/URL.html Host : docs.oracle.com Path : /javase/7/docs/api/java/net/URL.html Port : -1 Protocol : https Query : null Reference (Anchor) : getContent() User Info : null
No comments:
Post a Comment