[Solved] Saving a web page to a file in Java [closed]


This is HTTP. You can’t just open a socket and start reading something. You have to be polite to the server and send a request first:

socket.getOutputStream().write("GET /index.html HTTP/1.0\n\n".getBytes());
socket.getOutputStream().flush();

Then read a HTTP response, parse it, and get your html page back.

EDIT I wrote what to do with sockets only because it was the immediate problem of the OP. Using URLConnection is the correct way, as answered by @Mike Deck.

2

solved Saving a web page to a file in Java [closed]