Link to home
Start Free TrialLog in
Avatar of R6
R6

asked on

How to handle http post method?

hi experts,
im working on a simple web server program that meant to handle post method.  But, i just realized that the entity body of the post message, that is, the name and value pairs is not ended with crlf...so that means i cant use bufferedreader.readline to read it, because it will wait indefinitely.. So, what is the alternative??

thanks

r6
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

The best way is to look at how an open source web server handles this. Try:

http://jetty.mortbay.org/jetty/
Avatar of R6
R6

ASKER

hi CEHJ,

im sorry i cant find relevent topics from the link...sorry.  Is there any other way that i can 'read' from browser instead of using inputFromClient.readLine() such that (bufferedreader inputFromClient = new BufferedReader(new InputStreamReader(connectionSocket.getInputStream)));)??

many thanks

r6
ASKER CERTIFIED SOLUTION
Avatar of Mick Barry
Mick Barry
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of R6

ASKER

hi objects,

u mean first read the specified Content-Length value and then use this value in inputFromClient.read(cbuf,0,contentLengthInt), where inputFromClient is of BufferedReader right?
Also, when would the content length of the message be 1? when get and head method are encountered?

many thanks

r6
yes thats right. Content length will be -1 when it is unknown in which case you read until eof.
You can have a look at: http://www.onjava.com/lpt/a/3578
which explains about the HTTP protocol (including POST command) with code example for the http server and client.
For more about the HTTP protocol (without reading the rfc) -> http://www.jmarshall.com/easy/http/
>> when would the content length of the message be 1?

You mean -1? That would be the case for dynamically generated pages like JSPs. For static HTML pages, you will have a positive value of content-length.
mayankeagle, Though your examples are correct I am not sure if they are relevant in this case
where the receiver is an http server.
From the RFC http://www.w3.org/Protocols/HTTP/1.0/draft-ietf-http-spec.html it seems that the
client must send content-length with HTTP POST commands (server should indicate an error if that is
not the case).
Sorry my earlier comments regards -1 were refering to the response, not the request.
For your case you can most likely assume that content length will be specified (though there are some special cases when it may not be, or should be ignored).
>> Though your examples are correct I am not sure if they are relevant in this case where the receiver is an http server

Ah, you're right there. I meant about cases when the response comes from the server to the client..... guess I read the question too fast ;)
Avatar of R6

ASKER

hi objects,

sorry for this late rewarding, i thought i had done it already. sorry again

many thanks r6
thats ok :)