URI_URL1.htm
Uniform Resource Locator /Information
Objectives :
  • URI to URL
    • A URI has two specializations known as URL and URN.
  • Name URI is more technically correct
  • URISyntaxException or MalformedURLException, for malformed url
  • Parsing a URL
    • getPort, -1 means port is not set
Snapshots:

main() method with IO Exception

 

Complete Code:

/**
* This is Template 1 workplace
*/
import java.net.*;
import java.util.Date;
import java.io.*;

public class ClassTemplate1 {

public ClassTemplate1() {
// TODO Auto-generated constructor stub
}

public static void main(String[] args) throws IOException
{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

String str = "http:";
System.out.print("Type URL: ");
str = br.readLine();
process_this pt = new process_this();
pt.method_parse_url(str);
}

}

class process_this
{

public void method_parse_url(String str)throws IOException
{

try {
String str1 = str;
URI uri = new URI("http",str1,"/index.htm", "");
URL aURL = uri.toURL();
URLConnection ucon = aURL.openConnection();
System.out.println("Date : = " + new Date(ucon.getDate()));
System.out.println("protocol = " + aURL.getProtocol());
System.out.println("Connection type = " + ucon.getContentType());
System.out.println("authority = " + aURL.getAuthority());
System.out.println("Expiration = " + ucon.getExpiration());
System.out.println("Last Modified = " + ucon.getLastModified());
System.out.println("host = " + aURL.getHost());
System.out.println("port = " + aURL.getPort());
System.out.println("path = " + aURL.getPath());
System.out.println("query = " + aURL.getQuery());
System.out.println("filename = " + aURL.getFile());
System.out.println("ref = " + aURL.getRef());
} catch (URISyntaxException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//URL aURL = new URL(str);

}
}
 

Runtime view as with URI;