0% found this document useful (0 votes)
90 views

Practical 15

This document contains an advanced Java programming practical assignment. It includes code to retrieve the protocol, port, host, and file from a URL. It also includes code using the URL and URLConnection classes to retrieve the date, content type, and content length of a given URL. The output of running the code is displayed.

Uploaded by

shrub125io
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)
90 views

Practical 15

This document contains an advanced Java programming practical assignment. It includes code to retrieve the protocol, port, host, and file from a URL. It also includes code using the URL and URLConnection classes to retrieve the date, content type, and content length of a given URL. The output of running the code is displayed.

Uploaded by

shrub125io
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/ 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