Android应用程序仍然登录到网站,饼干?会议? [英] Android app remain logged into website, cookies? Session?

查看:136
本文介绍了Android应用程序仍然登录到网站,饼干?会议?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望做一个Android应用程序,基本上是一个基于文本的游戏网站的自定义视图。我知道该怎么做HttpPosting和这样的,所以发送的登录信息相对简单。但我的问题是,我将如何去然后导航网站?我从来没有真正合作过的客户端会话和饼干。是一个cookie来实现这个正确的方式?如何传递的信息返回给服务器访问后续页面时?

I'm looking to make an android app that is basically a custom view of a text based gaming website. I know how to do HttpPosting and such, so sending login information is relatively simple. But my question is, how would I go about then navigating the site? I've never really worked with sessions and cookies on the client side. Is a cookie the right way to implement this? How do I pass the info back to the server when accessing subsequent pages?

我希望是有道理的。

推荐答案

一般情况下,在Java中的HttpURLConnection可以设置/得到一个cookie这种方式(这里是整个连接过程)。下面的code是在我ConnectingThread的run(),使所有连接的活动类继承。它发送的所有请求都有着共同的静态sCookie字符串。因此,你可以保持一个共同的国家一样被记录开/关:

Generally, in Java HttpURLConnection you can set / get a cookie this way (here is the whole connection process). The code below is in my ConnectingThread's run(), from which all the connecting activity classes inherit. All share common static sCookie string which is sent with all the requests. Therefore you can maintain a common state like being logged on / off:

        HttpURLConnection conn = (HttpURLConnection) url.openConnection();             

        //set cookie. sCookie is my static cookie string
        if(sCookie!=null && sCookie.length()>0){
            conn.setRequestProperty("Cookie", sCookie);                  
        }

        // Send data
        OutputStream os = conn.getOutputStream(); 
        os.write(mData.getBytes());
        os.flush();
        os.close(); 

        // Get the response!
        int httpResponseCode = conn.getResponseCode();         
        if (httpResponseCode != HttpURLConnection.HTTP_OK){
           throw new Exception("HTTP response code: "+httpResponseCode); 
        }

        // Get the data and pass them to the XML parser
        InputStream inputStream = conn.getInputStream();                
        Xml.parse(inputStream, Xml.Encoding.UTF_8, mSaxHandler);                
        inputStream.close();

        //Get the cookie
        String cookie = conn.getHeaderField("set-cookie");
        if(cookie!=null && cookie.length()>0){
            sCookie = cookie;              
        }

        /*   many cookies handling:                  
        String responseHeaderName = null;
        for (int i=1; (responseHeaderName = conn.getHeaderFieldKey(i))!=null; i++) {
            if (responseHeaderName.equals("Set-Cookie")) {                  
            String cookie = conn.getHeaderField(i);   
            }
        }*/                

        conn.disconnect();                

这篇关于Android应用程序仍然登录到网站,饼干?会议?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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