0

post メソッドを使用して php ベースの api に接続するクライアント j2me アプリを作成しています。セキュリティ上の理由から、アプリと API はセッション内で 30 分のタイムアウトで対話する必要があります。私の問題は、セッション タイムアウトがまだ完了していない場合でも、アプリケーション ユーザーがログインし続けなければならないことです。私のphpコードは、ブラウザでテストしたので問題ありません。正常に動作します。しかし、アプリケーションは失敗し、ログインし続ける必要があります。何か不足している可能性がありますか? これらは、Java アプリを使用してサーバーに送信しているヘッダーです。

String phonenumber = this.phonenumberTextbox.getString();
String password = this.passwordTextBox.getString();
HttpConnection httpConn = null;
String url = "http://192.168.56.1/?action=login-user";
InputStream is;
OutputStream os;
is = null;
os = null;
String sReply = "";
boolean loggedIn = false;
try {
    // Open an HTTP Connection object
    httpConn = (HttpConnection) Connector.open(url);
    // Setup HTTP Request to POST
    httpConn.setRequestMethod(HttpConnection.POST);
    httpConn.setRequestProperty("User-Agent", "Profile/MIDP-1.0 Confirguration/CLDC-1.0");
    httpConn.setRequestProperty("Accept_Language", "en-US");
    //Content-Type is must to pass parameters in POST Request
    httpConn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
           os = httpConn.openOutputStream();
    String params;
    params = "phonenumber=" + phonenumber + "&password=" + password;
    os.write(params.getBytes());

    // Read Response from the Server
    StringBuffer sb = new StringBuffer();
    is = httpConn.openDataInputStream();
    int chr;
    while ((chr = is.read()) != -1) {
        sb.append((char) chr);
    }
    // Web Server just returns stuff         
    sReply = sb.toString();
} catch (IOException ex) {
    System.out.println("Cannot find server");
} finally {
    if (is != null) {
        try {
            is.close();
        } catch (IOException ex) {
        }
    }
    if (os != null) {
        try {
            os.close();
        } catch (IOException ex) {
        }
    }
    if (httpConn != null) {
        try {
            httpConn.close();
        } catch (IOException ex) {
        }
    }
}
// do something with sReply
4

1 に答える 1

1

最近の PHP でのデフォルトの慣例** は、PHP がクライアントが保存する Cookie を生成する場合にセッションを処理するときです。Java クライアントはこの Cookie (デフォルトでは phpsessid と呼ばれますが、ソルトに値する PHP プログラマーは Cookie 名を変更します) を保存し、後続の要求で HTTP ヘッダーに提示する必要があります。

私は Java での HTTP サポートにはあまり詳しくありませんが、curl のようなものであれば、Cookie を設定および取得するためのオプションが含まれています。

** 以前は、リクエストを行うときにクエリ文字列 (GET パラメーター) によってセッション トークンを渡すことができましたが、これは恐ろしく安全ではなく、機密情報が漏洩する可能性があるため、使用されなくなり、.オプション。

于 2013-08-29T11:42:45.543 に答える