I am supposed to develop a program that reads a web page with the specified URL, but the problem is that I am not allowed to use any HTTP libraries, I am only allowed to use TCP.
Below is the code that reads the response message:
private static String readMultiline (BufferedReader inStr) {
String message="";
String line=readLine(inStr);
while (line != null) {
message += line + "\r\n";
line=readLine(inStr);
}
if (message.length() == 0) return null;
return message;
}
private static String readLine (BufferedReader inStr) {
String line = null;
try{
line = inStr.readLine();
} catch (IOException ioe) {
System.out.println("ERROR: Incoming packet could not be read properly.");
return null;
}
return line;
}
The problem is that the header and the web page content are completely received, but the while loop still waits for the 'next' line, which does not exist. After a while, timeout occurs and the code continues. The server I am trying to connect to does not do "Connection:close". How can I detect the end of the file?