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.