I am writing a server that receives http request(only GET method as part of simplification from school work)
I used Socket
class to get the connection
I then used InputStream
as well as Scanner
to read the http request.
However, while I was reading the http request headers line by line using hasNext()
. The program hangs at hasNext()
, waiting for more inputs even it has consumed all the lines.
Below is my readRequest
method:
public void readRequest(Socket client) throws BadRequestException {
StringBuilder builder = new StringBuilder();
try {
Scanner sc = new Scanner(client.getInputStream());
sc.useDelimiter("\\r\\n");
while(sc.hasNext()){
builder.append(sc.next());
builder.append("\n");
}
parseRequestFromClient(builder.toString());
} catch (IOException e) {
throw new BadRequestException(e.getMessage());
}