Cannot get J2ME HTTP POST to work

Hi,

Can someone tell me why this code is not working. I am using JBuilder2006 with integrated Sun wireless toolkit, the latest version I believe. I am using a simple Tomcat JSP page to read the POST and send a response.  

GET operations work fine. But on the POST, the jsp recieves the request but the post body is not making it through. The jsp does report that the request type as post. The username and password parameters are null within the JSP.

Please help!

HttpConnection c = (HttpConnection)Connector.open("http://localhost:8080/WebModule2/jsp1.jsp",Connector.READ_WRITE);
String content = "username=beta&password=tester\r\n";
c.setRequestMethod(HttpConnection.POST);
c.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
OutputStream os = c.openOutputStream();
os.write(content.getBytes());
os.flush();
os.close();
   
StringBuffer sb = new StringBuffer();
InputStream is = c.openDataInputStream();
byte b[] = new byte[1024];
int cnt = 0;
long ts = System.currentTimeMillis();
while ( cnt > 0 || System.currentTimeMillis()-ts < 3000)
{
   cnt = is.read(b,0,b.length);
   if(cnt>0)
   {
     sb.append(new String(b,0,cnt));
     ts = System.currentTimeMillis();
   }
}
 response = sb.toString();
LVL 3
dgbwebAsked:
Who is Participating?
 
bgloddeCommented:
With a GET request, you don't have to send extra info, but with a POST, you have some extra requirements.
I'm assuming you are already launching this in it's own Thread, outside of the main thread, since your GETs are working.
Let's try setting the content-length of the POST first...

String clen = Integer.toString(content.length());
c.setRequestProperty("Content-length", clen);

See if that allows the body to return.
0
 
dgbwebAuthor Commented:
that fixed it

Thanks.

I have another question (see separate post for more points :-)

0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.