Cannot get J2ME HTTP POST to work

Posted on 2006-03-30
Last Modified: 2008-02-01

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)"http://localhost:8080/WebModule2/jsp1.jsp",Connector.READ_WRITE);
String content = "username=beta&password=tester\r\n";
c.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
OutputStream os = c.openOutputStream();
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 =,0,b.length);
     sb.append(new String(b,0,cnt));
     ts = System.currentTimeMillis();
 response = sb.toString();
Question by:dgbweb
    LVL 13

    Accepted Solution

    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.
    LVL 3

    Author Comment

    that fixed it


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


    Featured Post

    Do You Know the 4 Main Threat Actor Types?

    Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

    Join & Write a Comment

    Coaxial cable bending There are several factors that govern the selection of coaxial cable for your Machine to Machine (M2M) application: the location of cable runs, either indoor or outdoor, inside or outside an enclosure, maximum bending and the…
    With the purchase of CloudCommand by Comcast customers are left in a bind as subscriptions expire and render the AP's disabled. The following will explain how to flash your Ubiquiti AP's with CloudCommand firmware back to Ubiquiti firmware. HOWTO…
    This Micro Tutorial will show you how to maximize your wireless card to its maximum capability. This will be demonstrated using Intel(R) Centrino(R) Wireless-N 2230 wireless card on Windows 8 operating system.
    Viewers will learn how to connect to a wireless network using the network security key. They will also learn how to access the IP address and DNS server for connections that must be done manually. After setting up a router, find the network security…

    746 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    19 Experts available now in Live!

    Get 1:1 Help Now