Problems obtaining POST data / content using Sockets

Hi,

I have a servlet which needs to listen on a specific port for HTTP post requests.

At present I have the following code:

try{

inputStream=client.getInputStream();

streamReader=new InputStreamReader(inputStream);

reader=new BufferedReader(streamReader);

}catch(IOException e){

//Write error entry in the log file.

}

//Get the request and HOPEFULLY the params...

try{

System.out.println("encoder "+streamReader.getEncoding());

for (String thisLine = reader.readLine();thisLine!=null;thisLine = reader.readLine()){

if (thisLine!=null){

System.out.println(thisLine);

}

}

This produces the following....

POST /sms HTTP/1.1

Accept: image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/vnd.ms-

powerpoint, application/vnd.ms-excel, application/msword, */*

Referer: http://localhost/test.html

Accept-Language: en-gb

Content-Type: application/x-www-form-urlencoded

Accept-Encoding: gzip, deflate

User-Agent: Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)

Host: 127.0.0.1:9999

Content-Length: 74

Connection: Keep-Alive

Cache-Control: no-cache

__

I can see that the content is there 74 bytes of it, and it is encoded application/x-www-form-urlencoded, however I don't understand what I need to do to get the FORM information?

It works fine if it is an HTTP Get, however I have to get it to work with a POST.

Can Anyone help?

Thanks

Glenn

[2246 byte] By [gmdale1] at [2007-9-26 4:20:01]
# 1
I can't see anything wrong with the code you posted. The only thing I can think of is that perhaps you have this code inside of the doGet method. If that is the case change the method name to doPost or service.
amishslayer at 2007-6-29 17:22:59 > top of Java-index,Archived Forums,New To Java Technology Archive...
# 2

Nope, this is entirely outside the doGet & doPost.

Its basically a totally seperate class which the servlet owns. so in a way is entirely unconnected to the servlet (i.e. the servlet is just a method of instantiating it).

I'm a tad confused. I at least expected a few more lines containing the content? is this what normally happens?

Hope this helps?

thanks

Glenn

gmdale1 at 2007-6-29 17:22:59 > top of Java-index,Archived Forums,New To Java Technology Archive...
# 3
well, there is a servlet getting the request and then it streams the info over to this one, right? How does the initial servlet handle the request? Could you post that code as well?
amishslayer at 2007-6-29 17:22:59 > top of Java-index,Archived Forums,New To Java Technology Archive...
# 4

No,

the servlet doGet & doPost methods are entire unaffected / unused in this process.

I have an HTTPServer class that extends thread and opens and listens to a specific port using java.net.ServerSocket. When an http request is received I can accept this, pass the inputStream to the client code you saw above and hence have the inputstream to interrogate.

This works fine when it is a get since the querysting is sent as part of the header info you saw in the previous posting. However when I want to POST the info (Using just a standard html file to achieve this) I get the result I posted - i.e. no paramters...?

gmdale1 at 2007-6-29 17:22:59 > top of Java-index,Archived Forums,New To Java Technology Archive...
# 5

OK, I havn't worked with that class, but I have a pretty good guess.

With servlets the request are handle by HttpServletRequest and it is an interface so the implemenation changes. That means that a get can act differently from a post. And they do. This may be the case for you.

I had disappearing data too, and it turned out that the implementation of a post (at least for servlets) included deleting the info after it was referenced, they did that to preserve the intended functionality of the post method. So I had to put the info immediatly into another object and then reference it as I wanted.

This may or may not be your problem. Hope this helps.

amishslayer at 2007-6-29 17:22:59 > top of Java-index,Archived Forums,New To Java Technology Archive...
# 6
gmdale1,Can you post output that you get when you use GET instead of POST ?
neville_sequeira at 2007-6-29 17:23:00 > top of Java-index,Archived Forums,New To Java Technology Archive...
# 7

This is what I get when i do a GET.

GET /?msisdn=param1&type=param2&sitename=param3&pass=param4 HTTP/1.1

Accept: image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/vnd.ms-

powerpoint, application/vnd.ms-excel, application/msword, */*

Accept-Language: en-gb

Accept-Encoding: gzip, deflate

User-Agent: Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)

Host: localhost:9999

Connection: Keep-Alive

You can see that after the GET I get the querystring and so can parse this to get the parameters...

gmdale1 at 2007-6-29 17:23:00 > top of Java-index,Archived Forums,New To Java Technology Archive...
# 8
In the output you provided earlier and latest, I do not see the output from your code line...System.out.println("encoder "+streamReader.getEncoding());
neville_sequeira at 2007-6-29 17:23:00 > top of Java-index,Archived Forums,New To Java Technology Archive...
# 9
apologies, encoder Cp1252
gmdale1 at 2007-6-29 17:23:00 > top of Java-index,Archived Forums,New To Java Technology Archive...
# 10

Unlike GET, when using POST, the data part does not come in on the first line.

When using POST, the data follows after the all the headers. In fact there is also an explicit empty line between the headers part and the data part. Does this ring a bell. Have you taken this into consideration ?

I am asking because, from the your code it is not known as to where exactly you are getting the input stream from. In other words, in your code, what is client in client.getInputStream() Is it possible that the input strema you getting from client is already corrupted ?

neville_sequeira at 2007-6-29 17:23:00 > top of Java-index,Archived Forums,New To Java Technology Archive...
# 11

client is a java.net.Socket.

Found some further information:

I'm actually getting an exception I've just noticed from the log files see below:

java.net.SocketException: Connection reset by peer: JVM_recv in socket input stream read

at java.net.SocketInputStream.socketRead(Native Method)

at java.net.SocketInputStream.read(Unknown Source)

at java.net.SocketInputStream.read(Unknown Source)

at java.io.InputStreamReader.fill(Unknown Source)

at java.io.InputStreamReader.read(Unknown Source)

at java.io.BufferedReader.fill(Unknown Source)

at java.io.BufferedReader.readLine(Unknown Source)

at java.io.BufferedReader.readLine(Unknown Source)

at com.nokia.wap.server.IM.service.hd.run(hd.java:108)

--

the offending line in my code from above is line 108:

for (String thisLine = reader.readLine();thisLine!=null;thisLine = reader.readLine()){

though i'm not sure if this is a code problem / when I close the html browser and hence cancel the http request?!

sorry to add to the situation. :o(

gmdale1 at 2007-6-29 17:23:00 > top of Java-index,Archived Forums,New To Java Technology Archive...
# 12

The plot thickens.

I don't think there is a problem with thhat code line.

And I say that only because similar code has worked for me in the past. That is typically how one would read line-by-line from an input stream.

Now, what is this about closing the browser ? Are you closing the browser ? If so, why ? Am I missing something here ?

Yes, the socket connection will be closed when the browser is closed.! And the server would not work to completion.! So why are yu losing the broser ?!

neville_sequeira at 2007-6-29 17:23:00 > top of Java-index,Archived Forums,New To Java Technology Archive...
# 13
Hi,If u use the get method the form data is submitted through the querystring.But when u use the post method the form data is transfered in the body of the HTTP request.
ravitandur at 2007-6-29 17:23:00 > top of Java-index,Archived Forums,New To Java Technology Archive...
# 14

Only closing the browser once the request has been received in the servlet.

The servlet reads and displays the header of the request virtually instantly, there is no repsonse from the app so the browser (ie5) obviously continues to wait for a response. This is why i just end up closing it after about a minute / 30 secs.

Thinking further I dont think this is the reason why I cannot get the parameters though?!

If the POST data is in the body of the request, why do I keep getting an exception when I read the lines?

am baffled

:-(

gmdale1 at 2007-6-29 17:23:00 > top of Java-index,Archived Forums,New To Java Technology Archive...
# 15

PROBLEM RESOLVED ;-)

The answer is that the content / final line of the request is merely encoded as standard ASCII (DEC) values preceded with the ASCII (DEC) 13 & 10.

For some reason when the BufferedReader does a readLine() on the content it comes up blank? As a work around I've opted to read them and convert them to chars / string.

thanks again...

Glenn

gmdale1 at 2007-7-1 10:54:21 > top of Java-index,Archived Forums,New To Java Technology Archive...
# 16
How did you resolve this problem? Could you show me your code? Thanks
dongxiaowu at 2007-7-1 10:54:21 > top of Java-index,Archived Forums,New To Java Technology Archive...
# 17
Could you please share the code with us?
xfyang at 2007-7-1 10:54:22 > top of Java-index,Archived Forums,New To Java Technology Archive...