REST API 経由でサーバーに接続できる Nokia SDK 2.0 (J2ME) を使用して、S40 用のアプリを開発しています。
ただし、一部の 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 ボディの一部であり、Cookie は POST ヘッダーの一部です。そして、常に403エラーになります。
コードを作成すると、apigee (ウェブ) と私のアプリ (j2me アプリ) で異なる応答が返される可能性はありますか? もしそうなら、これを解決する方法は?
どんな助けにも感謝します。:)