Name: Ashwin Pawar Roll No.:: Advanced Java Programming

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 4

Advanced Java Programming

Name : ASHWIN PAWAR CO5I Roll No. : 23

Practical : 15

1. Execute the following code and write the output

OUTPUT:

Q. Write a program using URL class to retrieve the host, protocol, port and
file of URL http://www.msbte.org.in ?
CODE:
import java.net.URL;

public class practical15A{


public static void main(String[] args) {
String urlsString = "http://www.msbte.org.in";

try {
URL url = new URL(urlsString);

System.out.println("URL" + urlsString);
System.out.println("Host" + url.getHost());
System.out.println("Protocol" + url.getProtocol());
Advanced Java Programming

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

} catch (Exception e) {
System.out.println("ERROR" + e.getMessage());
e.getStackTrace();
}
}
}

OUTPUT:

Q. Write a program using URL and URLConnection class to retrieve the date,
content type, content length information of any entered URL?
CODE:
import java.net.URL;
import java.net.URLConnection;
import java.util.Date;

public class URLInfo {


public static void main(String[] args) {
String urlString = "http://www.udemy.com"; // Replace with the URL you want to
inspect
try {
Advanced Java Programming

URL url = new URL(urlString);


URLConnection connection = url.openConnection();

// Get and print the date


long dateMillis = connection.getDate();
if (dateMillis != 0) {
Date date = new Date(dateMillis);
System.out.println("Date: " + date);
} else {
System.out.println("Date not available.");
}

// Get and print the content type


String contentType = connection.getContentType();
if (contentType != null) {
System.out.println("Content Type: " + contentType);
} else {
System.out.println("Content Type not available.");
}
// Get and print the content length
int contentLength = connection.getContentLength();
if (contentLength != -1) {
System.out.println("Content Length: " + contentLength + " bytes");
} else {
System.out.println("Content Length not available.");
}
} catch (Exception e) {
System.err.println("Error: " + e.getMessage());
e.printStackTrace();
}
}
Advanced Java Programming

} OUTPUT:

You might also like