0% found this document useful (0 votes)
26 views9 pages

Pra14 15

Uploaded by

Priti Mane
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
26 views9 pages

Pra14 15

Uploaded by

Priti Mane
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 9

Practical 14

Name: Hivare Shraddha Pandit Roll No:24/35-018

Program Code:

FactoryMethod

import java.net.*;

public class FactoryMethodDemo

public static void main(String[] args)

try

InetAddress addr1 = InetAddress.getLocalHost();

System.out.println("Local Host IP: " + addr1);

InetAddress addr2 = InetAddress.getByName("msbte.org.in");

System.out.println("msbte.org.in IP: " + addr2);

InetAddress addr3[] = InetAddress.getAllByName("www.google.com");

for (int i = 0; i < addr3.length; i++) {

System.out.println("Google IP " + (i + 1) + ": " + addr3[i]);

catch (UnknownHostException e)

System.err.println("Error: " + e.getMessage());

}
}

OUTPUT:

Local Host IP: Dnyaneshwari/192.168.30.81

msbte.org.in IP: msbte.org.in/4.247.181.90

Google IP 1: www.google.com/142.250.183.4

Google IP 2: www.google.com/2404:6800:4009:820:0:0:0:2004

InstanceMethod

import java.net.*;

class InstanceMethodDemo

public static void main(String args[]) throws UnknownHostException

InetAddress addr1=InetAddress.getByName("localhost");

InetAddress addr2=InetAddress.getByName("localhost");

System.out.println("\n Is "+addr1+" equals "+addr2+"?"+addr1.equals(addr2));

System.out.println("\n Host name of "+addr1+" is "+addr1.getHostName());

System.out.println("\n Is "+addr1+" a multicast Address?"+addr1.isMulticastAddress());

System.out.println("\n Conservation of "+addr1+" to String is "+addr1.toString());

}
OUTPUT:

PS D:\AJP\>

Is mahaDBT.org.in/199.59.243.227 equals msbte.org.in/4.247.181.90?false

Host name of mahaDBT.org.in/199.59.243.227 is mahaDBT.org.in

Is mahaDBT.org.in/199.59.243.227 a multicast Address?false

Conservation of mahaDBT.org.in/199.59.243.227 to String is mahaDBT.org.in/199.59.243.227

Exercise:

import java.net.*;

import java.util.*;

public class ExerciseDemo

public static void main(String[] args)

try

InetAddress addr1 = InetAddress.getLocalHost();

System.out.println("Local Host IP: " + addr1.getHostAddress());

System.out.println("Enter Host Name:");

Scanner sc = new Scanner(System.in);

String hostName = sc.nextLine();

InetAddress addr2 = InetAddress.getByName(hostName);

System.out.println("IP Address of " + hostName + ": " + addr2.getHostAddress());

catch (UnknownHostException e)

{
System.err.println("Error: " + e.getMessage());

OUTPUT:

Local Host IP: 192.168.64.81

Enter Host Name:

Dnyaneshwari

IP Address of Dnyaneshwari: 192.168.64.81


Practical 15
Name: Hivare Shraddha Pandit Roll No:24/35-018

Class URL

import java.io.*;

import java.net.*;

class URLDemo

public static void main(String args[])

if (args.length != 1)

System.out.println("usage: java URLDemo URL");

return;

String st = args[0];

try

URL url = new URL(st);

System.out.println("Authority: " + url.getAuthority());

System.out.println("File: " + url.getFile());

System.out.println("Host: " + url.getHost());

System.out.println("Port: " + url.getPort());

System.out.println("Protocol: " + url.getProtocol());

System.out.println("Query: " + url.getQuery());

System.out.println("Ref: " + url.getRef());


System.out.println("User Info: " + url.getUserInfo() + '\n');

InputStream is = url.openStream();

int ch;

while ((ch = is.read()) != -1)

System.out.print((char) ch);

is.close();

catch (MalformedURLException e)

System.out.println("Malformed URL: " + e.getMessage());

e.printStackTrace(); // Print stack trace for debugging

catch (IOException e)

System.out.println("I/O Error: " + e.getMessage());

e.printStackTrace(); // Print stack trace for debugging

catch (Exception e)

System.out.println("General Error: " + e.getMessage());

e.printStackTrace(); // Print stack trace for debugging

}
}

OUTPUT:

usage: java URLDemo URL

URLConnection

import java.io.*;

import java.net.*;

import java.util.ArrayList;

import java.util.Date;

import java.util.HashMap;

import java.util.List;

import java.util.Map;

public class URLConnectionDemo

public static void main(String args[])

try

URL url = new URL("https://msbte.org.in");

URLConnection urlcon = url.openConnection();

System.out.println(urlcon.getAllowUserInteraction());

System.out.println(urlcon.getContentType());

System.out.println(urlcon.getURL());

System.out.println(urlcon.getDoInput());
System.out.println(urlcon.getDoOutput());

System.out.println(new Date(urlcon.getLastModified()));

System.out.println(urlcon.getContentEncoding());

Map<String, List<String>> header = urlcon.getHeaderFields();

for (Map.Entry<String, List<String>> entry : header.entrySet())

String key = entry.getKey();

List<String> values = entry.getValue();

System.out.println(key + ": " + values);

System.out.println();

catch (Exception e)

System.out.println(e);

OUTPUT:

false

text/html

https://msbte.org.in

true

false

Tue Oct 01 18:31:32 IST 2024


null

Accept-Ranges: [bytes]

null: [HTTP/1.1 200 OK]

Server: [Microsoft-IIS/10.0]

ETag: ["7e5d8d9214db1:0"]

Last-Modified: [Tue, 01 Oct 2024 13:01:32 GMT]

Content-Length: [832224]

Date: [Wed, 02 Oct 2024 14:11:48 GMT]

X-Powered-By: [ASP.NET]

Content-Type: [text/html]

You might also like