douju1852 2013-08-29 11:36
浏览 32

成功的php会话需要什么?

I'm writing a client j2me app that connects to a php based api using a post method. For security purposes the app and the api should interact within a session with a 30 minutes timeout. My problem is that, the application user has to keep on logging in even when the session time out is not yet done. My php code is fine because I've tested it on a browser and it works fine; however but the application fails and I have to keep on logging in. Might I be missing something? These are the headers am sending to the server using my Java app.

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
  • 写回答

1条回答 默认 最新

  • douwei8096 2013-08-29 11:42
    关注

    Default practice in PHP these days** when dealing for sessions if for PHP to generate a cookie for the client to store. Your java client must store this cookie (called phpsessid by default, but any PHP programmer worth their salt will change the cookie name), and present it in the HTTP headers on subsequent requests.

    I'm not very familiar with the HTTP support in Java, but if it's anything like curl it will include options for setting and retrieving cookies.

    ** It used to be possible to pass session tokens by a query string (a GET parameter) when making the request, but as this was horribly insecure and leaked potentially sensitive information, it's never used any more and may even no longer be available as an option.

    评论

报告相同问题?

悬赏问题

  • ¥15 帮我写一个c++工程
  • ¥30 Eclipse官网打不开,官网首页进不去,显示无法访问此页面,求解决方法
  • ¥15 关于smbclient 库的使用
  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教