开发者

HTTP POST does not return expected JSON response

I have pasted a code snippet for HTTP Post where I am POSTING a multipart message to the server which needs Authentication. I am expecting a JSON response, but when I run this I always get the login page in HTML.

public final class MyScreen extends MainScreen {
 private RichTextField _Output;
 public MyScreen() {
 // Set the displayed title of the screen
 setTitle("MyTitle");
 _Output = new RichTextField();
 add(_Output);
 addMenuItem(_GetDataAction);
}


 protected MenuItem _GetDataAction = new MenuItem("GetData", 100000, 10) {
 public void run() {
  String URL = "<Sample URL Goes Here>";
  ServiceRequestThread svc = new ServiceRequestThread(URL,
  (MyScreen) UiApplication.getUiApplication()
   .getActiveScreen());
  svc.start();
  }
 };


   public void updateDestination(final String text) {
  UiApplication.getUiApplication().invokeLater(new Runnable() {
   public void run() {
  _Output.setText(text);
   }
    });
    }
    }


   class ServiceRequestThread extends Thread {
   protected String _URL;
   protected MyScreen _Dest = null;
   protected URLEncodedPostData _PostData = null;
   StringBuffer writer = new StringBuffer();
   public void setPOSTData(URLEncodedPostData data) {
   _PostData = data;
    }
    public ServiceRequestThread(String URL, MyScreen screen) {
    super();
    _Dest = screen;
    _URL = URL;
     }
        public void run() {

         try
         {
    String boundary = "SATBA";
     String twoHyphens = "--";
     String data1 = "{\"IMPORTING\":{ \"IN_COUNTRY_CODE\":\"US\"}}";
     String CRLF = "\r\n";
    byte[] encoded = Base64OutputStream.encode
    ("User:password".getBytes(), 0, "User:password".length(), false,false);

    "Prepare the data for post"

     writer.append("--" + boundary).append(CRLF);
     writer.append("Content-Disposition: form-data; name=\"param\"").append(
     CRLF);
     writer.append("Content-Type: text/json; charset=" + "UTF-8").append(CRLF);
     writer.append("Content-Transfer-Encoding: 8bit").append(CRLF);
     writer.append("Request-Id:Abcd123456" ).append(CRLF);
     writer.append("Request-Type:rfc_json").append(CRLF);
     writer.append("function:00163E0136C01EE0AE8B059433A71727")
     .append(CRLF);
      writer.append(CRLF);
     writer.append(data1).append(CRLF);
      writer.append("--" + boundary + "--").append(CRLF);
     String string = new String(writer);

    HttpConnection conn1 = (HttpConnection)Connector.open(_URL,Connector.READ_WRITE);
   conn1.setRequestMethod(HttpConnection.POST);
  conn1.setRequestProperty("Authorization", "Basic "+ new String(encoded));
  conn1.setRequestProperty("Content-Type","multipart/mixed; boundary="开发者_运维技巧 + boundary);

   OutputStreamWriter osw = new OutputStreamWriter(conn1.openOutputStream(), "UTF-8");
   osw.write(string);
   osw.flush();
   osw.close();

   int responseCode = conn1.getResponseCode();
   if (responseCode == HttpConnection.HTTP_OK) {
   InputStream data = conn1.openInputStream();
   StringBuffer raw = new StringBuffer();
   byte[] buf = new byte[4096];
   int nRead = data.read(buf);
   while (nRead > 0) {
   raw.append(new String(buf, 0, nRead));
   nRead = data.read(buf);
    }
   _Dest.updateDestination(raw.toString());
    } else {
   _Dest.updateDestination("responseCode="
   + Integer.toString(responseCode));
     }
    }
     catch( IOException e)
       {
    e.printStackTrace();
   _Dest.updateDestination("Exception:"+e.toString());
    }
     }
            }


Turns out the code was perfectly alright and the issue was on the rim.public property file where the application.handler.http.AuthenticationSupport was set to true and because of this it was not loggging in.

Now I set it to false and get the correct response.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜