2

我的应用程序进行多次网络调用以获得身份验证。我需要将此会话存储在 cookie 中。我想使用 Cookie Manager,但经过一些研究,我发现它仅适用于 API 9 及更高版本,并且我的应用程序需要向后兼容。

我使用 HTTPURLConnection 连接到安全的 HTTPS。我的代码的快速示例

    public String iStream_to_String(InputStream is)
{
    BufferedReader rd = new BufferedReader(new InputStreamReader(is), 4096);
    String line;
    StringBuilder sb = new StringBuilder();
    try
    {
        while ((line = rd.readLine()) != null)
        {
            sb.append(line);
        }
        rd.close();

    } catch (IOException e)
    {
        e.printStackTrace();
    }
    String contentOfMyInputStream = sb.toString();
    return contentOfMyInputStream;
}

final static HostnameVerifier DO_NOT_VERIFY = new HostnameVerifier()
{
    public boolean verify(String hostname, SSLSession session)
    {
        return true;
    }
};

/**
 * Trust every server - dont check for any certificate
 */
private static void trustAllHosts()
{
    // Create a trust manager that does not validate certificate chains
    TrustManager[] trustAllCerts = new TrustManager[]
    { new X509TrustManager()
    {
        public java.security.cert.X509Certificate[] getAcceptedIssuers()
        {
            return new java.security.cert.X509Certificate[]
            {};
        }

        public void checkClientTrusted(X509Certificate[] chain,
                String authType) throws CertificateException
        {
        }

        public void checkServerTrusted(X509Certificate[] chain,
                String authType) throws CertificateException
        {
        }
    } };

    // Install the all-trusting trust manager
    try
    {
        SSLContext sc = SSLContext.getInstance("TLS");
        sc.init(null, trustAllCerts, new java.security.SecureRandom());
        HttpsURLConnection
                .setDefaultSSLSocketFactory(sc.getSocketFactory());
    } catch (Exception e)
    {
        e.printStackTrace();
    }
}

然后我提出这样的要求

    try
    {
        url = new URL(url1);



        trustAllHosts();
           HttpsURLConnection https = (HttpsURLConnection) url.openConnection();
       https.setHostnameVerifier(DO_NOT_VERIFY);
        http = https;

        InputStream in = new BufferedInputStream(http.getInputStream());

        sAuthenticateP1 = iStream_to_String(in);
        in.close();


    } catch (Exception e)
    {
        e.printStackTrace();
    }

完整的身份验证分 4 个步骤完成。我需要它,以便在整个 4 个步骤中记住会话。看到我不能使用 CookieManager,我一直在寻找其他方法,但似乎找不到任何方法。谁能指出我正确的方向。

提前致谢!!

4

1 回答 1

1

弄清楚了。万一其他人有类似的问题,将给出代码的快速概述。正如我之前所说,我的身份验证过程有几个步骤。所以在第一个请求之后,在你收到响应之后,像这样获取 cookie

String cookie = http.getRequestProperty("Cookie");
            if (cookie != null && cookie.length() > 0)
            {
                sCookie = cookie;
                Log.v("cookie2", sCookie);
            }

sCookie 是我设置的静态字符串变量。然后在下一个请求中,在这一行之后

https = (HttpsURLConnection) url.openConnection(); 

就放

            https.setRequestProperty("Cookie", sCookie);
            https.setRequestMethod("POST");
            https.setDoInput(true);
            https.setDoOutput(true);

并且在需要会话之后为每个请求做同样的事情,它应该可以正常工作

于 2012-05-09T12:29:31.497 回答