Java program of URL connection to get Port ,get Content Type, get Last Modified etc.
import java.net.*;
import java.io.*;
import java.util.Date;
class pst15
{
public
static void main(String str[])throws Exception
{
URL
u=new URL("http://presidentofindia.nic.in/speeches.html");
URLConnection
uc=u.openConnection();
System.out.println("Date:"+
new Date(uc.getDate()));
System.out.println("port
is"+u.getPort());
System.out.println("type
of content:"+ uc.getContentType());
System.out.println("Expire:"+
uc.getExpiration());
System.out.println("date
of last modified:"+ new Date(uc.getLastModified()));
int
len=uc.getContentLength();
System.out.println("length"+len);
}
}
/*output:
payal@payal-HP-Notebook:~$ javac pst15.java
payal@payal-HP-Notebook:~$ java pst15
Date:Thu Jan 01 05:30:00 IST 1970
type of content:null
Expire:0
date of last modified:Thu Jan 01 05:30:00 IST 1970
length-1
*/
java program to perform to understand the InetAddress in Advance java
import java.net.InetAddress;
import java.net.UnknownHostException;
public class pst27
{
public
static void main(String arg[])
{
try
{
InetAddress
local=InetAddress.getLocalHost();
String
address=local.getHostAddress();
String
hostName=local.getHostName();
System.out.println("local
host IP address:"+address);
System.out.println("local
host name:"+hostName);
}
catch(UnknownHostException
e)
{
e.printStackTrace();
}
}
}
/*
payal@payal-HP-Notebook:~$ javac pst27.java
payal@payal-HP-Notebook:~$ java pst27
local host IP address:127.0.1.1
local host name:payal-HP-Notebook
*/
Java Program to perform some of the function of URL class
import java.net.*;
import java.io.*;
public class pst28
{
public
static void main(String obj[])
{
try
{
URL
u=new URL("http://www.tutorialpoint.com/java8/index.htm");
System.out.println("URL
is"+u.toString());
System.out.println("protocol
is"+u.getProtocol());
System.out.println("authority
is"+u.getAuthority());
System.out.println("file
name is"+u.getFile());
System.out.println("host
is"+u.getHost());
System.out.println("path
is"+u.getPath());
System.out.println("port
is"+u.getPort());
System.out.println("default
port"+u.getDefaultPort());
System.out.println("query
is"+u.getQuery());
System.out.println("ref
is"+u.getRef());
System.out.println("External
form:"+u.toExternalForm());
}
catch(Exception
e)
{
e.printStackTrace();
}
}
/*output
payal@payal-HP-Notebook:~$ javac URLDemo.java
payal@payal-HP-Notebook:~$ java URLDemo
URL ishttp://www.tutorialpoint.com/java8/index.htm
protocol ishttp
authority iswww.tutorialpoint.com
file name is/java8/index.htm
host iswww.tutorialpoint.com
path is/java8/index.htm
port is-1
default port80
query isnull
ref isnull
*/
}