android - 重用 HttpURLConnection 以保持 session Activity

标签 android httpurlconnection sessionid

我们有一个 Android 应用程序,它要求用户输入验证码的答案。 验证码是在我们的服务器上生成的。 回复后发送到服务器进行验证。

问题是因为我必须在请求验证码后关闭 HttpURLConnection,然后我发现回复正在服务器上的不同 session 上运行。 因此,验证码检查失败,因为它依赖于 session 。

有没有办法保持连接,或者我应该走另一条路? 我知道在等效的 iPhone 应用程序中,它们保持“连接”状态,因此具有相同的 sessionid。

编辑:

    CookieManager cookieManager = new CookieManager();  
    CookieHandler.setDefault(cookieManager);

    URL urlObj = new URL(urlPath);
    conn = (HttpURLConnection) urlObj.openConnection();

    if (urlPath.toLowerCase().startsWith("https:")) {
        initializeHttpsConnection((HttpsURLConnection) conn);
    }
    conn.setRequestMethod("POST");
    conn.setRequestProperty("Content-Language", "en-US");
    conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
    conn.setRequestProperty("Content-Length", Integer.toString(bodyData.length));
    if (_sessionIdCookie != null) {
        conn.setRequestProperty("Cookie", _sessionIdCookie);
    }
    // Connect
    conn.setDoInput(true);
    conn.setDoOutput(true);
    conn.connect();

最佳答案

通常, session 不会基于 http 连接本身来保持。那没有任何意义。 session 通常通过客户端的 cookie 和服务器端的 session 信息保持 Activity 状态。 您要做的是保存您收到的 cookie,然后在您下次连接到服务器时设置该(那些)cookie。

要了解有关如何使用 HttpUrlConnection 类处理 session 和 cookie 的更多信息,请阅读文档:http://developer.android.com/reference/java/net/HttpURLConnection.html

这里有一小段摘录可以帮助您入门:

To establish and maintain a potentially long-lived session between client and server, HttpURLConnection includes an extensible cookie manager. Enable VM-wide cookie management using CookieHandler and CookieManager:

CookieManager cookieManager = new CookieManager();  
CookieHandler.setDefault(cookieManager);

编辑:

对于使用 API 级别 8 或更低级别的用户,您需要使用 Apache 的库!

引用代码如下:

 // Create a local instance of cookie store
    CookieStore cookieStore = new BasicCookieStore();

    // Create local HTTP context
    HttpContext localContext = new BasicHttpContext();
    // Bind custom cookie store to the local context
    localContext.setAttribute(ClientContext.COOKIE_STORE, cookieStore);

    HttpGet httpget = new HttpGet("http://www.google.com/"); 

    System.out.println("executing request " + httpget.getURI());

    // Pass local context as a parameter
    HttpResponse response = httpclient.execute(httpget, localContext);

以上代码取自 Apache 的库示例。在这里能找到它: http://svn.apache.org/repos/asf/httpcomponents/httpclient/trunk/httpclient/src/examples/org/apache/http/examples/client/ClientCustomContext.java

编辑 2: 说清楚:

对于 Apache 库,您需要通过 HttpContext 对象以某种方式“连接”cookie 管理对象和连接对象。

在 HttpUrlConnection 的情况下,这不是必需的。当您使用 CookieHandler 的静态方法 setDefault 时,您正在设置系统范围的 cookieHandler。以下是 CookieHandler.java 的摘录。请注意变量名称(来自 Android 开源项目 (AOSP) 存储库):

 37 /**
 38      * Sets the system-wide cookie handler.
 39      */
 40     public static void setDefault(CookieHandler cHandler) {
 41         systemWideCookieHandler = cHandler;
 42     }

关于android - 重用 HttpURLConnection 以保持 session Activity ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10714943/

相关文章:

javax.net.ssl.SSLHandshakeException : Trust anchor for certification path not found

java - Android pdf View 库中的错误

java - 空白 "Location" header ,但在 IE 中发生重定向

java - 我正在向服务器发送 GET 方法,但接收 POST 方法

java - 从 HttpURLConnection 对象解析 JSON

php - session_destroy 没有取消设置 session_id

java - Android 6.0 发布时应用程序崩溃,包括 Logcat,新开发人员

php - 深层链接无法正常工作

php - 检查 PDO 中的 session ID 是否为 ISSET

c# - 如何重新打开或重置WebBrowser控件?