I am trying to write a program that requests an HTTP response and then writes it to a html file. Until here all good, in the html file I want to see only the HTML code and not the header. I tried with a if statement, saying that if the line starts with http || data || ......and so on do nothing otherwise write to a file. But it doesnt seem to be working. Can someone help me.
public static void main(String[] args) {
int port = 80;
Socket socket = null;
try {
String input = null;
BufferedReader user = new BufferedReader(new InputStreamReader(System.in));
System.out.print("Insert a website ");
input = user.readLine();
user.close();
socket = new Socket(input, port);
PrintWriter pw = new PrintWriter(socket.getOutputStream(), true);
String line= null;
pw.print("GET / HTTP/1.1\r\n");
pw.print("Host: www.google.com\r\n");
pw.print("\r\n");
pw.flush();
FileOutputStream fos = new FileOutputStream("indexxx.html");
PrintWriter ppw = new PrintWriter(fos);
System.out.println("Writing to file");
BufferedReader inn = new BufferedReader(new InputStreamReader(socket.getInputStream()));
while (true)
{
String writing = inn.readLine();
if (writing == null || writing.startsWith("HTTP")|| writing.startsWith("Server"))
{
}
else{
ppw.println(writing);
}
ppw.flush();
System.out.println("Written to file");
socket.close();}
} catch (UnknownHostException e) {
System.out.println(e);
} catch (IOException e1) {
System.out.println(e1);
} finally {
try {
if (socket != null)
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
Aucun commentaire:
Enregistrer un commentaire