0% found this document useful (0 votes)
15 views2 pages

Java HTTP Client TCP

The document outlines a Java program that downloads a web page using TCP sockets and saves it as an HTML file. The algorithm includes parsing the URL, connecting the socket, sending a GET request, reading the response, and saving the content to a file. The program successfully retrieves the web page and confirms the save with a message.

Uploaded by

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

Java HTTP Client TCP

The document outlines a Java program that downloads a web page using TCP sockets and saves it as an HTML file. The algorithm includes parsing the URL, connecting the socket, sending a GET request, reading the response, and saving the content to a file. The program successfully retrieves the web page and confirms the save with a message.

Uploaded by

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

AIM:

Download a web page using Java TCP sockets and save it as an HTML file.

ALGORITHM:

1. Parse URL

2. Connect socket

3. Send GET request

4. Read response

5. Save to file

JAVA PROGRAM:

import java.io.*;

import java.net.*;

public class Client {

public static void main(String[] args) throws Exception {

Socket s = new Socket("example.com", 80);

PrintWriter out = new PrintWriter(s.getOutputStream());

out.print("GET / HTTP/1.1\r\nHost: example.com\r\n\r\n");

out.flush();

BufferedReader in = new BufferedReader(new InputStreamReader(s.getInputStream()));

BufferedWriter file = new BufferedWriter(new FileWriter("page.html"));

String line; boolean content = false;

while ((line = in.readLine()) != null) {

if (line.isEmpty()) content = true;


else if (content) file.write(line + "\n");

file.close(); in.close(); s.close();

System.out.println("Saved as page.html");

OUTPUT:

Saved as page.html

RESULT:

Web page downloaded and saved using TCP sockets.

You might also like