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

HTTP Web Client

This document describes a Java experiment to: 1. Display HTTP header information using the URL class 2. Download a web page's source code using TCP sockets and the HTTPClient class. The experiment achieves the outcome of implementing protocols using TCP as required by the course.

Uploaded by

rishikesh 1
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
415 views4 pages

HTTP Web Client

This document describes a Java experiment to: 1. Display HTTP header information using the URL class 2. Download a web page's source code using TCP sockets and the HTTPClient class. The experiment achieves the outcome of implementing protocols using TCP as required by the course.

Uploaded by

rishikesh 1
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 4

Exp.

No: 3 HTTP CLIENT PROGRAMS


Date:

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.

CLASSES AND METHODS USED

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

<< draw the flow chart as your own >>

PROGRAM

import java.net.*;
import java.io.*;

public class ParseURL {


public static void main(String[] args) throws Exception {
URL aURL = new URL("http://java.sun.com:80/docs/books/tutorial" + "/index.html?
name=networking#DOWNLOADING");
System.out.println("protocol = " + aURL.getProtocol());
System.out.println("authority = " + aURL.getAuthority());
System.out.println("host = " + aURL.getHost());
System.out.println("port = " + aURL.getPort());
System.out.println("path = " + aURL.getPath());
System.out.println("query = " + aURL.getQuery());
System.out.println("filename = " + aURL.getFile());
System.out.println("ref = " + aURL.getRef());
}
}

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

<< draw the flow chart as your own >>


PROGRAM

import java.io.*;
import java.net.*;

public class HTTPClient {


public static void main(String[] args) {

String hostName = "www.google.com";


int portNumber = 80;

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

<html> <title> … hild,m=document.createElement("li");m.className=b+"


gbmtc";m.id=c;a.className="g
bmt";m.appendChild(a);if(k.hasChildNodes()){c=[["gbkc"],["gbf","gbe","gbn"],["gb
kp"],["gbnd"]];d=0;var n=k.childNodes.length;f=!1;for(var l=-1,q=0,E;E=c[q];q++)
{for(var U=0,I;I=E[U];U++){for(;d<n&&H(k.childNodes[d],I);)d++;if(I==b){k.insert
Before(m,k.childNodes[d]||
null);f=!0;break}}if(f){if(d+1<k.childNodes.length){var V=k.childNodes[d+1];H(V.
firstChild,"gbmh")||fb(V,E)||(l=d+1)}else if(0<=d-1){var W=k.childNodes[d-1];H(W
.firstChild,"gbmh")||fb(W,E)||(l=d)}break}0<d&&d+1<n&&d++}if(0<=l){var y=documen
t.createElement("li"),z=document.createElement("div");y.className="gbmtc";z.clas
sName="gbmt gbmh";y.appendChild(z);k.insertBefore(y,k.childNodes[l])}g.addHover&
&g.addHover(a)}else k.appendChild(m)}}catch(Eb){r(Eb,"sb","al")}},fb=function(a,
<a class=gbgt id=gbztm href="https://www.google.co.in/in
tl/en/about/products?tab=wh" onclick="gbar.tg(event,this)" aria-haspopup=true ar
ia-owns=gbd><span class=gbtb2></span><span id=gbztms class="gbts gbtsa"><span id
=gbztms1>More</span><span class=gbma></span></span></a><div class=gbm id=gbd ari
a-owner=gbztm><div id=gbmmb class="gbmc gbsb gbsbis"><ol id=gbmm class="gbmcc gb
sbic"><li class=gbmtc><a onclick=gbar.logger.il(1,{t:24}); class=gbmt id=gb_24 h
ref="https://www.google.com/calendar?tab=wc">Calendar</a></li><li class=gbmtc><a
onclick=gbar.logger.il(1,{t:51}); class=gbmt id=gb_51 href="http://translate.go
ogle.co.in/?hl=en&tab=wT">Translate</a></li><li class=gbmtc><a onclick=gbar.logg
er.il(1,{t:10}); class=gbmt id=gb_10 href="https://books.google.co.in/bkshp?hl=e
n&tab=wp">Books</a></li><li class=gbmtc><a onclick=gbar.logger.il(1,{t:6}); clas
s=gbmt id=gb_6 href="https://www.google.co.in/shopping?hl=en&source=og&tab=wf">S
hopping</a></li><li class=gbmtc><a onclick=gbar.logger.il(1,{t:30}); class=gbmt
id=gb_30 href="http://www.blogger.com/?tab=wj">Blogger</a></li><li class=gbmtc><
a onclick=gbar.logger.il(1,{t:31}); class=gbmt id=gb_31 href="https://photos.goo
gle.com/?tab=wq&pageId=none">Photos</a></li><li class=gbmtc><a onclick=gbar.logg
er.il(1,{t:25}); class=gbmt id=gb_25 href="https://docs.google.com/document/?usp
=docs_alc">Docs</a></li><li class=gbmtc><div class="gbmt gbmh"></div></li><li cl
ass=gbmtc><a onclick=gbar.logger.il(1,{t:66}); href="https://www.google.co.in/in
tl/en/about/products?tab=wh" class=gbmt>Even more &raquo;</a></li></ol><div clas
s=gbsbt></div><div class=gbsbb></div></div></div></li></ol></div><div id=gbg><h2
class=gbxx>Account Options</h2><span class=gbtcb></span><ol class=gbtc><li clas
s=gbt><a target=_top href="https://accounts.google.com/ServiceLogin?hl=en&passiv
e=true&continue=http://www.google.com/" onclick="gbar.logger.il(9,{l:'i'})" id=g
b_70 class=gbgt><span class=gbtb2></span><span id=gbgs4 class=gbts><span id=gbi4
s1>Sign in</span></span></a></li><li class="gbt gbtb"><span class=gbts></span></
li><li class=gbt><a class=gbgt id=gbg5 href="http://www.google.co.in/preferences
?hl=en" title="Options" onclick="gbar.tg(event,this)" aria-haspopup=true aria-ow
ns=gbd5><span class=gbtb2></span><span id=gbgs5 class=gbts><span id=gbi5></span>
</span></a><div class=gbm id=gbd5 aria-owner=gbg5><div class=gbmc><ol id=gbom cl
ass=gbmcc><li class="gbkc gbmtc"><a class=gbmt href="/preferences?hl=en">Search
settings</a></li><li class=gbmtc><div class="gbmt gbmh"></div></li><li class="g
function _F_installCss(c){}
(function(){google.spjs=false;google.snet=true;google.em=[];google.emw=false;})(
);google.sm=1;(function(){var pmc='{\x22Qnk92g\x22:{},\x22RWGcrA\x22:{},\x22U5B2
1g\x22:{},\x22YFCs/g\x22:{},\x22ZI/YVQ\x22:{},\x22d\x22:{},\x22mVopag\x22:{},\x2
2sb_he\x22:{\x22agen\x22:true,\x22cgen\x22:true,\x22client\x22:\x22heirloom-hp\x
22,\x22dh\x22:true,\x22dhqt\x22:true,\x22ds\x22:\x22\x22,\x22ffql\x22:\x22en\x22
,\x22fl\x22:true,\x22host\x22:\x22google.com\x22,\x22isbh\x22:28,\x22jsonp\x22:t
e\x22,\x22oskt\x22:\x22Input tools\x22,\x22psrc\x22:\x22This search was removed
from your \\u003Ca href\x3d\\\x22/history\\\x22\\u003EWeb History\\u003C/a\\u003
E\x22,\x22psrl\x22:\x22Remove\x22,\x22sbit\x22:\x22Search by image\x22,\x22srch\
x22:\x22Google Search\x22},\x22ovr\x22:{},\x22pq\x22:\x22\x22,\x22refpd\x22:true
,\x22rfs\x22:[],\x22sbpl\x22:24,\x22sbpr\x22:24,\x22scd\x22:10,\x22sce\x22:5,\x2
2stok\x22:\x223cuzwTiSlOjLuAn1tQE7Ih3ykNc\x22,\x22uhde\x22:false}}';google.pmc=J
SON.parse(pmc);})();</script> </body></html>
0

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.

You might also like