Question

I'm developing an apps for S40 using Nokia SDK 2.0 (J2ME) that can connect via REST API to the server.

However there's some API (using method POST) result an Error 403 - Forbidden. I've checked the API in the apigee site, with exactly the same header and body and the result was surprisingly success (200 OK response). Unfortunately I can't share the URL due to my client's confidential.

But i'm using this function:

public static String sendHTTPPOST(String url, String parameter, String cookie) {
    HttpConnection httpConnection = null;
    DataInputStream dis = null;
    DataOutputStream dos = null;
    StringBuffer responseMessage = new StringBuffer();
    try {
        httpConnection = (HttpConnection) Connector.open(url, Connector.READ_WRITE);
        httpConnection.setRequestMethod(HttpConnection.POST);
        httpConnection.setRequestProperty("User-Agent", "Profile/MIDP-1.0 Configuration/CLDC-1.0");
        httpConnection.setRequestProperty("Content-Type", "application/json");
    httpConnection.setRequestProperty("Cookie", "eid="+cookie);
        httpConnection.setRequestProperty("Content-length", "" + parameter.getBytes().length);

        dos = httpConnection.openDataOutputStream();
        byte[] request_body = parameter.getBytes();
        for (int i = 0; i < request_body.length; i++) {
            dos.writeByte(request_body[i]);
        }

        dos.flush();

    dis = new DataInputStream(httpConnection.openInputStream());
        int ch;
        while ((ch = dis.read()) != -1) {
            responseMessage.append((char) ch);
        }
    } catch (Exception e) {
        e.printStackTrace();
        responseMessage.append("ERROR");
    } finally {
        try {
            if (httpConnection != null) {
                httpConnection.close();
            }
            if (dis != null) {
                dis.close();
            }
            if (dos != null) {
                dos.close();
            }
        } catch (IOException ioe) {
            ioe.printStackTrace();
        }
    }
    return responseMessage.toString();
}

The parameter is the part of POST body, the cookie is part of the POST header. And it always result 403 Error.

Is there any possibility that make the code may result different response on the apigee (web) and my apps (j2me apps)? If so, how to solve this?

Will appreciate any help. :)

Was it helpful?

Solution

I got the answer, I should've checked more on the session. I forgot to encode it.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top