0% found this document useful (0 votes)
18 views3 pages

Java Practical No 24

The document contains Java code demonstrating the use of the URL class to retrieve and display various components of a URL, including protocol, host, port, and file. It also includes error handling for malformed URLs and connection issues. Additionally, it shows how to read and print the content from the specified URL using BufferedReader.

Uploaded by

dipya2121
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)
18 views3 pages

Java Practical No 24

The document contains Java code demonstrating the use of the URL class to retrieve and display various components of a URL, including protocol, host, port, and file. It also includes error handling for malformed URLs and connection issues. Additionally, it shows how to read and print the content from the specified URL using BufferedReader.

Uploaded by

dipya2121
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/ 3

Practical no 24

Title code:
import java.net.*;
public class URLDemo {
public static void main(String[] args) {
try {
URL url = new URL("https://www.example.com");
System.out.println(url.getProtocol());
System.out.println(url.getHost());
URLConnection urlConnection = url.openConnection();
System.out.println(urlConnection.getContentType());
} catch (MalformedURLException e) {
System.out.println("Invalid URL");
} catch (IOException e) {
System.out.println("Connection error");
}
}
}

O/P:

Practical Related Questions:


4. Write a program using URL class to
retrieve the host, protocol, port and file of
URL
http://www.msbte.org.in
Ans:
import java.net.*;
import java.io.*;
public class URLDemo {
public static void main(String[] args) {
try {
URL url = new URL("http://www.msbte.org.in");
System.out.println("Protocol: " + url.getProtocol());
System.out.println("Host: " + url.getHost());
System.out.println("Port: " + (url.getPort() == -1 ? url.getDefaultPort() :
url.getPort()));
System.out.println("File: " + url.getFile());
URLConnection urlConnection = url.openConnection();
System.out.println("Content Type: " + urlConnection.getContentType());
System.out.println("Content Length: " +
urlConnection.getContentLength());
System.out.println("Date: " + urlConnection.getDate());
System.out.println("Last Modified: " + urlConnection.getLastModified());

BufferedReader br = new BufferedReader(new


InputStreamReader(urlConnection.getInputStream()));
String line;
System.out.println("Content:");
while ((line = br.readLine()) != null) {
System.out.println(line);
}
br.close();
} catch (Exception e) {
System.out.println("Error: " + e.getMessage());
}
}
}

O/P:

You might also like