HTTP Web Client
HTTP Web Client
AIM:
This experiment provides the solution for the following problem statements using Java
a) Display the contents of the HTTP header information.
b) HTTP web client program to download a web page using TCP sockets .
Procedure:
URL is a way of naming resources and is mainly used to link pages in the World Wide Web. It
has three parts – Protocol used, host name, resource name. The classes available in Java are URL,
URLConnection. The format is
Protocol name://Host name:Port no/File path # Reference
URL can be either
Absolute URL - The complete URL is specified like
http://www.server1.com/dir1/index.html (or)
Relative URL - The URL is specified relative to base URL. For example if the web
page currently viewed is http://www.server1.com/dir1/index.html and to view
file1.html in the folder dir1, URL can be mentioned as /dir1/file1.html.
URL
Constructor:
public URL (String protocol, String host, int port, String file)
Creates a URL object from the specified protocol, host, port number, and file.
Methods:
InputStream openStream () - Opens a connection to this URL and returns an
InputStream for reading from that connection.
int getPort () -Returns the port number of this URL.
String getProtocol () -Returns the protocol name of this URL.
URLConnection
Constructor:
public URLConnection (URL url))
Constructs a URL connection to the specified URL.
Methods:
int getContentLength()- Returns the value of the content-length header
field(-1 if the length is unknown)
String getContentType()- Returns the value of the content-type header
field(null if the type is unknown)
long getExpiration () - Returns the value of the expires header field. (0 if not
known. The value is the number of milliseconds since January 1, 1970 GMT.)
long getLastModified () -Returns the value of the last-modified header field.
The result is the number of milliseconds since January 1, 1970 GMT.
long getDate() - Returns the value of the date header field.
a) Display the contents of the HTTP header information.
ALGORITHM
1.
2.
3.
FLOWCHART
PROGRAM
import java.net.*;
import java.io.*;
OUTPUT
protocol = http
authority = java.sun.com:80
host = java.sun.com
port = 80
path = /docs/books/tutorial/index.html
query = name=networking
filename = /docs/books/tutorial/index.html?name=networking
ref = DOWNLOADING
b) HTTP web client program to download a web page using TCP sockets .
ALGORITHM
1.
2.
3.
FLOWCHART
import java.io.*;
import java.net.*;
try {
Socket socket = new Socket(hostName, portNumber);
PrintWriter out =
new PrintWriter(socket.getOutputStream(), true);
BufferedReader in =
new BufferedReader(
new InputStreamReader(socket.getInputStream()));
out.println("GET / HTTP/1.1\nHost: www.google.com\n\n");
String inputLine;
while ((inputLine = in.readLine()) != null) {
System.out.println(inputLine);
}
} catch (UnknownHostException e) {
System.err.println("Don't know about host " + hostName);
System.exit(1);
} catch (IOException e) {
System.err.println("Couldn't get I/O for the connection to " +
hostName);
System.exit(1);
}
}
}
OUTPUT
RESULT
This experiment created a java application with TCP socket class and URL class for
extracting information from HTTP protocol header and concern web client’s home page source code
information could be downloaded.
Outcome:
The course outcome (CO1: Implement various protocols using TCP and UDP) has been
attained by these java applications.