0% found this document useful (0 votes)
31 views4 pages

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

This document discusses Java programming concepts. It provides code examples to retrieve information from URLs using the URL and URLConnection classes, including the host, protocol, port, file, date, content type and content length.

Uploaded by

gmpawar003
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)
31 views4 pages

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

This document discusses Java programming concepts. It provides code examples to retrieve information from URLs using the URL and URLConnection classes, including the host, protocol, port, file, date, content type and content length.

Uploaded by

gmpawar003
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 : 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