J2ME App not sending POST requests

2019-05-20 23:45发布

问题:

I am a bit confused right now.

I have a J2ME app that sends POST requests to a servlet. It was working up until miraculous this morning it stopped working. It just won't send the POST data. It will contact the server, but the request data will be empty.

System.out.println(request.getParameterMap());

Always returns

{}

I tried to check the network monitor from the emulator and I saw this

sq~wLhttp://localhost:80802xѬ2xѬ????????xsq~wLhttp://localhost:80802xѬ2xѭ????????xsq~z?http://localhost:80802xѬK2xѬ????????1316274753996
POST /amw/synch HTTP/1.1
User-Agent: Profile/MIDP-1.0 Confirguration/CLDC-1.0 UNTRUSTED/1.0
Accept_Language: en-US
Content-Type: application/x-www-form-urlencoded
Host: localhost:8080
Transfer-Encoding: chunked

56
&action=recover&email=trinisoftinc@gmail.com&password=1011001&api-key=oalkuisnetgauyno
0

xsq~z?http://localhost:80802xѬD2xѭ????????1316274754009
HTTP/1.1 200 OK
X-Powered-By: Servlet/3.0 JSP/2.2 (GlassFish Server Open Source Edition 3.1 Java/Apple Inc./1.6)
Server: GlassFish Server Open Source Edition 3.1
Content-Type: text/html;charset=UTF-8
Content-Length: 46
Date: Sat, 17 Sep 2011 15:52:33 GMT

{"message":"Server Error: null","status":500}

I didn't know what to make of it really.

Please I need help.

The code sending the request is below

public String post(String url, String query, String optionalParameters) throws IOException {
    if (optionalParameters != null) {
        url += optionalParameters;
    }

    query += "&api-key=" + APIKey;
    Echo.outln(url + ", " + query.getBytes().length);
    Echo.outln(query);
    HttpConnection connection;
    connection = (HttpConnection) Connector.open(url);

    connection.setRequestMethod(HttpConnection.POST);
    connection.setRequestProperty("User-Agent", "Profile/MIDP-1.0 Confirguration/CLDC-1.0");
    connection.setRequestProperty("Accept_Language", "en-US");
    connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
    connection.setRequestProperty("Content-Length", String.valueOf(query.getBytes().length));
    //connection.setRequestProperty("api-key", APIKey);

    OutputStream os = connection.openDataOutputStream();
    os.write(query.getBytes());
    os.flush();
    if (connection.getResponseCode() == HttpConnection.HTTP_OK) {
        InputStream is = connection.openInputStream();
        int ch;
        StringBuffer buffer = new StringBuffer();

        while((ch = is.read()) != -1) {
            buffer.append((char) ch);
        }            

        try {
            is.close();
            connection.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return buffer.toString();
    } else {
        String retval = connection.getResponseMessage() + " : " + connection.getResponseCode();
        try {
            connection.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return retval;
    }
}

Thanks.

NB: I have an iPhone app that calls the same servlet and works perfectly well.

回答1:

So case matters. This post Http post from J2ME saved my life.

The offending line is

connection.setRequestProperty("Content-Length", "" + query.getBytes().length);

Instead of

connection.setRequestProperty("Content-length", "" + query.getBytes().length);

NOTE: Small letter l, not capital letter L.

After that change, the world looks beautiful again



回答2:

First of all, I think we can see the POST with some strange characters (xѬ2xѬ??). Can be good to see the right ones.

Next point is the POST seems to be sent but the response from the server is bad (look at the message "Server Error: null" with the 500 status). So, did you test to send the same post from a browser using a simple HTML form for example? And what about the server? Did you have analysed the stacktrace with the error? The request process in the server seems to have something wrong.



回答3:

Looking at your code, I really cant see what's wrong with it. I was able to use this snippet(find below) to make a POST request from a J2ME app to a Java back-end and everything worked well. I hope it works for you. All the best.

         HttpConnection httpConn = null;

        String url = "http://your_resource";        


        InputStream is = null;
        OutputStream os = null;
        String phonenumber ="080380012";
        String data = "Post data";
        try {



                httpConn = (HttpConnection) Connector.open(url);                     
                httpConn.setRequestMethod(HttpConnection.POST);
                httpConn.setRequestProperty("User-Agent",
                                "Profile/MIDP-1.0 Confirguration/CLDC-1.0");
                httpConn.setRequestProperty("Accept_Language", "en-US");                                    

                httpConn.setRequestProperty("Content-Type",
                                "application/x-www-form-urlencoded");          

                os = httpConn.openOutputStream();       
                String params;
                params = "URL=" + data;
                params += "&Phone=" + phonenumber;

                os.write(params.getBytes());


                os.flush();
    ///parsing the xml response.... this part depends on what you are returning from the server
                    XmlParser parser = new XmlParser(new InputStreamReader(httpConn.openInputStream()));
                org.kxml.kdom.Document doc = new org.kxml.kdom.Document();
                doc.parse(parser);
                   /// work with the parsed data
                }
                catch (Exception ex)
        {
                System.out.println(ex.getMessage()+ "exception Ex");

        }      

        finally {
                if (is != null)
                        is.close();
                if (os != null)
                        os.close();
                if (httpConn != null)
                        httpConn.close();
        }


回答4:

The best approach I know to post to an url is to use this tutorial on HttpMultipart. Try it !