We help IT Professionals succeed at work.

We've partnered with Certified Experts, Carl Webster and Richard Faulkner, to bring you a podcast all about Citrix Workspace, moving to the cloud, and analytics & intelligence. Episode 2 coming soon!Listen Now

x

Cannot get J2ME HTTP POST to work

dgbweb
dgbweb asked
on
Medium Priority
559 Views
Last Modified: 2008-02-01
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();
Comment
Watch Question

Commented:
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.

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts

Author

Commented:
that fixed it

Thanks.

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

Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.