Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: BufferedReader hangs when handling POST method data

  1. #1
    Junior Member
    Join Date
    Jan 2012
    Posts
    3
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default BufferedReader hangs when handling POST method data

    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


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: BufferedReader hangs when handling POST method data

    What type of pages are your writing? Servlet? jsp? Either contains an HttpServletRequest instance that has the necessary information already parsed and can be accessed via the getParameter() or getQueryString() method.

  3. #3
    Junior Member
    Join Date
    Jan 2012
    Posts
    3
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: BufferedReader hangs when handling POST method data

    Neither, really. I'm just opening a port with a ServerSocket, then when you connect through a web browser, I push very basic HTML/CSS using PrintWriter.

    There are other options I've come across, but this is the way it's being taught, so I figure this is the way he expects it to be done. Plus, the class isn't a Java class, it's a broad Web Applications class encompassing many languages. This section of the course isn't meant to teach java, but the underlying architecture of HTML, which I think is why it's being done this way.

  4. #4
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: BufferedReader hangs when handling POST method data

    Have you tried reading from the BufferedReader using just the read() or read(char[], off, length)?

  5. The Following User Says Thank You to copeg For This Useful Post:

    Sucker Punch (January 28th, 2012)

  6. #5
    Junior Member
    Join Date
    Jan 2012
    Posts
    3
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: BufferedReader hangs when handling POST method data

    Quote Originally Posted by copeg View Post
    Have you tried reading from the BufferedReader using just the read() or read(char[], off, length)?
    I've tried that, but I had difficulty with that as well.

    Using the read() method just resulted in a series in integers for some reason, and using read(char[], off, length) was throwing exceptions.

    FAKE EDIT: But now that I try it again, read(char[], off, length) is working perfectly. I must have screwed up somewhere when I was experimenting with it this morning.

    Thanks a lot.

Similar Threads

  1. POST method in a loop
    By kollyisrealisaac in forum Java ME (Mobile Edition)
    Replies: 0
    Last Post: July 26th, 2011, 11:29 AM
  2. Can't form a simple POST or GET http request. No data returns.
    By goodguy in forum What's Wrong With My Code?
    Replies: 1
    Last Post: May 29th, 2011, 05:04 AM
  3. Thread Hangs at split method.
    By kailasvilaskore in forum Threads
    Replies: 1
    Last Post: November 26th, 2010, 12:13 PM
  4. Java: Handling cookies when logging in with POST
    By cloakbot in forum What's Wrong With My Code?
    Replies: 0
    Last Post: June 16th, 2010, 04:31 PM
  5. (.readLine() method) of BufferedReader class
    By chronoz13 in forum File I/O & Other I/O Streams
    Replies: 7
    Last Post: October 13th, 2009, 06:59 PM