2

In my application, I am trying get the response using POST request. The response server is sending me in Json format. But after adding the properties, it is returning me the response code as 411 (i.e issue with content length). I have already added the content length. Then where is the issue i am not getting. Here is my code:

String url = "https://xxx:8243/people/v3";
STRURL = url + HttpComm.getConnectionString().trim();

StringBuffer postData = new StringBuffer();
HttpConnection httpConnection = null;
try {
    httpConnection = (HttpConnection) Connector.open(STRURL);
} catch (IOException e1) {
    e1.printStackTrace();
};

try {
    httpConnection.setRequestMethod("POST");
    postData.append("?username="+user);
    postData.append("&password="+password);
    String encodedData = postData.toString();
    byte[] postDataByte = postData.toString().getBytes("UTF-8");
    httpConnection.setRequestProperty("Authorization", "bearer"+"ZWOu3HL4vwaOLrFAuEFqsxNQf6ka");
    httpConnection.setRequestProperty("Content-Type","application/json");
    httpConnection.setRequestProperty("Content-Length", String.valueOf(postDataByte.length));
    OutputStream out = httpConnection.openOutputStream();
    DataOutputStream dos = new DataOutputStream(out);
    out.write(postData.toString().getBytes());
    out.flush();
    int statusCode = httpConnection.getResponseCode();
    Logger.out("HttpComm", "status code:::::::   "+statusCode);
    if (statusCode != HttpConnection.HTTP_OK)
    {

    }

Updated Code :

HttpConnection httpConnection = null;
    try {
        httpConnection = (HttpConnection) Connector.open(STRURL);
    } catch (IOException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    };
            try {
                httpConnection.setRequestMethod("POST");
                URLEncodedPostData postData = new URLEncodedPostData("UTF-8", false);
                 postData.append("username", user);
                 postData.append("password", password);
                 byte[] postDataByte = postData.getBytes();
                  httpConnection.setRequestProperty("Authorization", "bearer"+"ZWOu3HL4vwaOLrFAuEFqsxNQf6ka");
                   httpConnection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
                   httpConnection.setRequestProperty("Content-Length", String.valueOf(postDataByte.length));
                   OutputStream out = httpConnection.openOutputStream(); 
                   out.write(postDataByte);
                   out.flush();

                   int statusCode = httpConnection.getResponseCode();
                   Logger.out("HttpComm", "status code:::::::   "+statusCode);
Arindam Mukherjee
  • 2,255
  • 11
  • 48
  • 83
  • 1
    Surely out.write(postData.toString().getBytes()) is not correct, it should be out.write(postDataByte) – samlewis Jun 18 '13 at 19:01

1 Answers1

1

There are a few things that don't look quite right here. I would recommend trying this:

 httpConnection.setRequestMethod("POST");

 URLEncodedPostData postData = new URLEncodedPostData("UTF-8", false);
 postData.append("username", user);
 postData.append("password", password);
 byte[] postDataByte = postData.getBytes();

 httpConnection.setRequestProperty("Authorization", "bearer"+"ZWOu3HL4vwaOLrFAuEFqsxNQf6ka");
 httpConnection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
 httpConnection.setRequestProperty("Content-Length", String.valueOf(postDataByte.length));

 OutputStream out = httpConnection.openOutputStream(); 
 DataOutputStream dos = new DataOutputStream(out);
 out.write(postDataByte);
 out.flush();

 int statusCode = httpConnection.getResponseCode();
 Logger.out("HttpComm", "status code:::::::   "+statusCode);
 if (statusCode != HttpConnection.HTTP_OK)

What I changed:

  1. As @samlewis said, the code was creating a variable to hold the post data bytes, but then was not using it when it called out.write().

  2. The code set the content type to JSON, but it was not sending JSON. The request was simply two parameters. The response may be JSON, but you don't specify that in the request's Content-Type parameter.

  3. The username/password parameters were encoded just using strings. Normally, it's best to use the URLEncodedPostData class to hold your POST parameters.

  4. If you are going to use strings, I think it was still incorrect to add a ? to the front of the username parameter. If you want to encode parameters in a GET URL, then you use https://xxx:8243/people/v3?username=user&password=password. But, this code was using POST, not GET.

  5. There was also an unused encodedData variable.

Community
  • 1
  • 1
Nate
  • 31,017
  • 13
  • 83
  • 207
  • I have change the code as you said. But still i am getting the same response : 411. But when i am typing this url in the browser, i am getting the proper JSON response. https://api.yookos.com:8243/people/v3?username=[yookos-username]&password=[yookos-password] – Arindam Mukherjee Jun 21 '13 at 17:30
  • @Arindam, Do you have a **test** username and password for this server that you can share here? Without that, I can't test this for you, and I'm not sure what else I can do to help. You should make the changes I posted in my answer, but it's always possible that something else is going wrong, too. Thanks. – Nate Jun 21 '13 at 18:02
  • Instead of giving you the test user and password, i can give you the JSON response which i am getting after hitting the URL in the desktop browser. Will be it OK? – Arindam Mukherjee Jun 22 '13 at 06:48
  • @Arindam, if you have the JSON response, yes, it would be good to **edit** your question and put it in there. However, I don't think that's going to help debug the `411` error code. – Nate Jun 22 '13 at 08:54
  • @Arindam, what is `throw 'allowIllegalResourceCall is false.';`? That's not JSON. I'm also a little confused. If the server is returning that large JSON response, is it not working? Are you expecting something different? You're saying that you get a `411` error code, but you **also** get this JSON back? – Nate Jun 22 '13 at 09:07
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/32202/discussion-between-arindam-mukherjee-and-nate) – Arindam Mukherjee Jun 22 '13 at 09:10
  • So i am providing my user name and password in the desktop browser. And getting that response as in json format. And i don't know about that allowIllegalResourceCall. – Arindam Mukherjee Jun 22 '13 at 09:31
  • What you get back in the browser doesn't really help you here, though. The browser is almost certainly implemented correctly. Your BlackBerry code may not be, and that's what you're trying to debug. Again, I'm not sure that I can be of further help to you, if you've already tried the code I posted, **exactly** as it is above. – Nate Jun 22 '13 at 09:39