Results 1 to 4 of 4
  1. #1
    Join Date
    Mar 2009
    Posts
    4
    Rep Power
    0

    Default Problems Making a "POST" to a Socket

    I am having problems making a Post from an HTML page to a Socket. It appears that when I attempt to parse the input, the blank line between the header and data (line 6 of the HTML post example below) causes the "inputStream" never to return.

    Java Code:
    ....
      outputStream = new BufferedOutputStream(clientSocket.getOutputStream());
      InputStream inputStream = new BufferedInputStream(clientSocket.getInputStream());
    				
      StringBuilder sbRequest = new StringBuilder();
      while (true) {
    	int byRead = inputStream.read();
    	if (byRead == -1) break;	
    	sbRequest.append((char)byRead);
      }
      String sRequest = new String(sbRequest);
    ...
    An example of a HTML post

    Java Code:
    	POST /login.jsp HTTP/1.1
    	Host: www.mysite.com
    	User-Agent: Mozilla/4.0
    	Content-Length: 27
    	Content-Type: application/x-www-form-urlencoded
    
    	userid=joe&password=guessme
    Any help will be gratefully receive.

    Kind Regards,
    Harold Clements

  2. #2
    Join Date
    Mar 2009
    Posts
    4
    Rep Power
    0

    Default Re: Problems Making a "POST" to a Socket

    OK, it appears that the HTML is not closing the socket so "inputStream" still thinks that there is more data. Any ideas no how to close the "inputStream" and still have the "POST" data that proceeds it.

  3. #3
    coiner is offline Member
    Join Date
    Jan 2010
    Posts
    8
    Rep Power
    0

    Default Re: Problems Making a "POST" to a Socket

    I am having a similar issue. For some reason when the POST is sent using default form enctype "x-www-form-urlencoded" the POST query string is (seemingly) never received by the socket. However, when you use enctype "multipart/form-data" all of the data is successfully read by the socket. Is there perhaps some sort of ACK that needs to be sent to the client after the header is received? That doesn't make much sense but I don't get why the socket does not seem to receive the query string for a basic url encoded POST. It seems like such a straightforward thing but it's just like... why is there no query string being read???
    Last edited by coiner; 10-11-2011 at 10:14 PM.

  4. #4
    Grkn is offline Member
    Join Date
    Oct 2011
    Posts
    65
    Rep Power
    0

    Default Re: Problems Making a "POST" to a Socket

    I guess always same problem. :)

    First thing you can use time connection time.
    Socket s=new Socket(....);
    s.soTimeout(1000); //1000ms = 1second;
    After 1 second connection will be closed by this code.
    You can choose your time.

    Second thing
    You can use thread that helps you to listen always. If you use thread when listening inputs, your code make other stuffs as well.

Similar Threads

  1. Java Applet Help - Making "Snake" Game
    By Alphimeda in forum Java Applets
    Replies: 15
    Last Post: 04-04-2010, 05:39 PM
  2. Replies: 5
    Last Post: 04-02-2010, 04:01 PM
  3. Replies: 0
    Last Post: 12-29-2009, 03:10 PM
  4. Making A Set Of Classes "Importable"
    By JDCAce in forum Advanced Java
    Replies: 4
    Last Post: 12-05-2008, 09:11 AM
  5. Replies: 1
    Last Post: 10-20-2008, 07:35 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •