ajp_prac_15
ajp_prac_15
15
Program code: Execute the following code and write the output.
import java.net.*;
class URLDemo
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:
C:\Internship\url>javac URLDemo.java
C:\Internship\url>java URLDemo
Protocol : http
Port : -1
Host : www.javatpoint.com
File : /javafx-tutorial
Ext : http://www.javatpoint.com/javafx-tutorial
Exercise:
1) Write a program using URL class to retrieve the host, protocol, port and file of the URL
http://www.msbte.org.in
import java.net.*;
class saee_15_1
System.out.println("Protocol : "+hp.getProtocol());
System.out.println("Port : "+hp.getPort());
System.out.println("Host : "+hp.getHost());
System.out.println("File : "+hp.getFile());
Output:
C:\Internship\url>javac saee_15_1.java
C:\Internship\url>java saee_15_1
Protocol : http
Port : -1
Host : www.msbte.org.in
File :
2) Write a program using URL and URLConnection class to retrieve the data, content
type, content length information of any entered URL.
import java.io.*;
import java.net.*;
class saee_15_2
long d = obj.getDate();
if (d == 0) {
} else {
d = obj.getExpiration();
if (d == 0) {
} else {
d = obj.getLastModified();
if (d == 0) {
} else {
int l = obj.getContentLength();
if (l == -1) {
} else {
Output:
C:\Internship\url>javac saee_15_2.java
C:\Internship\url>java saee_15_2
Date: 1728061416000