Android的会话cookie,而无需使用CookieManager [英] Android Session cookies without using CookieManager

查看:155
本文介绍了Android的会话cookie,而无需使用CookieManager的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用程序,使多个Web调用,以获得认证。我需要这个会话存储在cookie中。我想使用Cookie管理,但做一些研究之后,我发现它仅适用于API 9以上,我的应用程序需要向后兼容。

My application makes multiple web calls in order to get authentication. I need to store this session in a cookie. I wanted to use Cookie Manager but after doing some research, I found out it is only available to API 9 and above and my application needs to be backward compatible.

我使用HttpURLConnection类到一个安全的HTTPS让我的网络连接。我的code的快速例子

I make my web connections using HTTPURLConnection to a secure HTTPS. Quick example of my code

    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();
    }
}

然后我做出像这样的请求

Then I make a request like so

    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,我一直在四处寻找这样做的其他方式,但似乎无法找到任何。会有人能指出我在正确的方向。

The full authentication is done in 4 steps. I need to have it so the session is remembered throughout the 4 steps. Seeing I can't use CookieManager, I have been looking around for other ways of doing this, but can't seem to find any. Would anyone be able to point me in the right direction.

在此先感谢!

推荐答案

想通了。柜面其他人是否有类似的问题,将给予code的快速轮廓。正如我以前说过我的是几个步骤认证过程。因此,第一个请求后,您收到响应后,取饼干像这样

Figured it out. Incase anyone else is having similar problem, will give quick outline of code. As I said before mine is a several step authentication process. So after the first request, after you have received a response, take the cookie like so

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

sCookie是一个静态的字符串变量我已成立。然后在接下来的要求,该行之后

sCookie is a static string variable I have set up. Then in the next request, after this line

https = (HttpsURLConnection) url.openConnection(); 

只要把

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

和做每个请求同样的事情,需要在会议结束后,它应该正常工作

And do the same thing for each request after that requires the session, and it should work fine

这篇关于Android的会话cookie,而无需使用CookieManager的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆