【发布时间】:2013-03-14 16:50:38
【问题描述】:
我正在使用诺基亚 SDK 2.0 (J2ME) 为 S40 开发一个应用程序,该应用程序可以通过 REST API 连接到服务器。
但是有一些 API(使用方法 POST)导致错误 403 - 禁止。我检查了 apigee 站点中的 API,其标头和正文完全相同,结果出人意料地成功(200 OK 响应)。很遗憾,由于客户的机密,我无法分享 URL。
但我正在使用这个功能:
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();
}
参数是POST body的一部分,cookie是POST header的一部分。 并且总是导致 403 错误。
是否有可能使代码在 apigee(网络)和我的应用程序(j2me 应用程序)上产生不同的响应? 如果有,如何解决?
将不胜感激任何帮助。 :)
【问题讨论】:
标签: java-me http-status-code-403 httpconnection nokia-s40