I'd got a simple problem that has been driving me crazy for a couple hours now. Google has shown me what (I think) the problem is, but hasn't helped me fix it. I'm working on an assignment which is to create a very basic server program, all it does is allow a user to login with a pre-made username and password.
It's working up until the user enters their information and hits Submit. The page uses the POST method to transmit the data to the server, where I have a method that outputs the header information in the console and returns the attached login information. The problem seems to be that there is an empty line between the end of the headers and the beginning of the message. readLine() only returns a line if it ends in a line feed or carriage return, and I guess that little gap doesn't have one, because it hangs at that point.
Here is the method in question, more or less. I've been playing around with it for a while, this is the simplest it's been. Boolean p is whether or not it is a POST method or GET method. It's false when the server receives anything other than a POST method, and that works fine.
public static String printRequest(BufferedReader r, boolean p) throws IOException { String login = ""; String line = r.readLine(); while (!line.equals("")) { System.out.println(line); line = r.readLine(); } if (p) login = r.readLine(); System.out.println(); return login; }
After that while loop executes, it hangs on the next readLine() indefinitely. I know the server has received the data, because if I click "Submit" again, it does then continue and print out the buffer in the console, and the username/password information is at the top, before the request I made from hitting Submit a second time. For some reason I just can't seem to get past that blank line to the relevant information.
Does anyone know how to get around this? Or am I completely barking up the wrong tree and there's some other problem I've run into?
EDIT: If it's helpful, this is what is (or should be) in the BufferedReader when the method executes. I had a tough time explaining my problem.
POST / HTTP/1.1 Host: localhost:8002 Connection: keep-alive Content-Length: 35 Cache-Control: max-age=0 Origin: http://localhost:8002 User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/535.7 (KHTML, like Gecko) Chrome/16.0.912.77 Safari/535.7 Content-Type: application/x-www-form-urlencoded Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8 Referer: http://localhost:8002/ Accept-Encoding: gzip,deflate,sdch Accept-Language: en-GB,en-US;q=0.8,en;q=0.6 Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.3 username=usr-test&password=pwd-test