Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
EX:NO:
HTTP WEBCLIENT PROGRAM TO DOWNLOAD A WEBPAGE
USING TCP SOCKETS
DATE:
AIM:
To Write a java program to download a webpage using TCP sockets.
ALGORITHM:
STEP1:Create a URL object and pass url as string to download the webpage.
STEP2:Create Buffered Reader object and pass openstream().Method of URL in InputStream
object.
STEP3:Create a string object to read each line one by one from stream.
STEP4:Write each line in html file where webpage will be downloaded.
STEP5:Close all objects
STEP6:Catch exception if url failed to download.
SOURCE CODE:
import java.io.*;
import java.net.*;
public class webpagedown
{
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);
BufferedReaderin=new BufferedReader(newInputStreamReader(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);
NAME: DELHIBABU.G REGNO:513417104012 YEAR/SEM:III Yr/ V sem
}
}
}
OUTPUT:
NAME: DELHIBABU.G REGNO:513417104012 YEAR/SEM:III Yr/ V sem
NAME: DELHIBABU.G REGNO:513417104012 YEAR/SEM:III Yr/ V sem
NAME: DELHIBABU.G REGNO:513417104012 YEAR/SEM:III Yr/ V sem
RESULT:
T
hus the java program to download a web page has been downloaded
successfully.