import java.net.MalformedURLException; import java.net.URL; /** * Program to print all the parts of the given url * * @author sahir maredia (Kotia Solutions) * */ public class URLParts { /** * @param args */ public static void main(String[] args) { if (args.length != 1) { System.out.println("Please provide valid arguments"); return; } try { URL url = new URL(args[0]); System.out.println("====== Given URL is : " + args[0] + " ======"); System.out.println("Protocol : " + url.getProtocol()); // if port not specified as part of url, getting default port int port = url.getPort() == -1 ? url.getDefaultPort() : url .getPort(); System.out.println("Port : " + port); System.out.println("Host : " + url.getHost()); System.out.println("Path : " + url.getPath()); System.out.println("File Name : " + url.getFile()); System.out.println("Query : " + url.getQuery()); } catch (MalformedURLException e) { System.out.println(e.getMessage()); } } }
Run this program as follows :
sahir@sahir-laptop:~/networklab$ javac URLParts.java
sahir@sahir-laptop:~/networklab$ java URLParts http://www.kotiasolutions.com/services.html?service=web
====== Given URL is : http://www.kotiasolutions.com/services.html?service=web ======
Protocol : http
Port : 80
Host : www.kotiasolutions.com
Path : /services.html
File Name : /services.html?service=web
Query : service=web
sahir@sahir-laptop:~/networklab$
sahir@sahir-laptop:~/networklab$ java URLParts http://www.kotiasolutions.com/services.html?service=web
====== Given URL is : http://www.kotiasolutions.com/services.html?service=web ======
Protocol : http
Port : 80
Host : www.kotiasolutions.com
Path : /services.html
File Name : /services.html?service=web
Query : service=web
sahir@sahir-laptop:~/networklab$
No comments:
Post a Comment