-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathURL_Operation.java
34 lines (34 loc) · 941 Bytes
/
URL_Operation.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
import java.net.*;
public class URL_Operation
{
public static void main(String args[])
{
try
{
InetAddress Lhost= InetAddress.getLocalHost();
String hostname = Lhost.getHostName();
System.out.println("Hostname using InetAddress:"+hostname);
URL u=new URL("https://www.google.com");
//String host = u.getHost();
System.out.println("Hostname using URL class:"+u.getHost());
System.out.println("Protocol:"+u.getProtocol());
System.out.println("Host:"+u.getHost());
System.out.println("Port:"+u.getPort());
System.out.println("Path:"+u.getPath());
System.out.println("Query:"+u.getQuery());
System.out.println("Ref:"+u.getRef());
URLConnection c=u.openConnection();
c.connect();
String cHostname = c.getURL().getHost();
System.out.println("Hostname using URLConnection: "+cHostname);
}
catch(UnknownHostException | MalformedURLException e)
{
e.printStackTrace();
}
catch(Exception e)
{
e.printStackTrace();
}
}
}