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

Advanced Java Programming

Name : Pranjal Shahane CO5I Roll No. : 06

Practical : 15

1. Execute the following code and write the output


import java.net.*;
class URLDemo
{
public static void main(String args[]) throws MalformedURLException
{
URL hp = new URL("https://www.javatpoint.com/javafx-tutorial");
System.out.println("Protocol: " + hp.getProtocol());
System.out.println("Port: " + hp.getPort());
System.out.println("Host: " + hp.getHost());
System.out.println("File: " + hp.getFile());
System.out.println("Ext:" + hp.toExternalForm());
}
}
OUTPUT:
Advanced Java Programming

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());
System.out.println("Port" + url.getPort());
System.out.println("File" + url.getFile());

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

OUTPUT:
Advanced Java Programming

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.example.com"; // Replace with the URL you want to
inspect
try {
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);
Advanced Java Programming

} 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();
}
}
} OUTPUT:

You might also like