多语言展示
当前在线:1863今日阅读:19今日分享:20

安卓中cookie如何使用

Cookie发明的,为了网站的服务器端辨别用户的身份,保持session会话而保存到用户本地磁盘上的用户数据,该数据是经过加密的。Cookie是由服务器端生成,发送给客户端的(User-Agent一般是浏览器),客户端浏览器会将Cookie的key/value保存到某个目录下的文本文件内,下次请求同一网站时就发送该Cookie给服务器(前提是浏览器必须设置启用了cookie),服务器就可以知道该用户是否合法用户以及是否需要重新登录等。代码如下:public static JSONObject HttpRequest(String method,String callback,Map paramesMap)            throws Exception{        ..............         HttpPost request = new HttpPost(Global.SERVER_URL);        request.setEntity(new UrlEncodedFormEntity(params, HTTP.UTF_8));        request.addHeader('Pragma', 'no-cache');        request.addHeader('Cache-Control', 'no-cache');        request.addHeader('Cookie', Global.cookie);        //Global是一个全局类,存放一些应用程序系统全局的变量什么的。        .................       HttpClient client = new DefaultHttpClient(getHttpParams());       HttpResponse httpResponse = client.execute(request);       //得到服务器端传过来的Cookie对象,存在Header中       Header header = httpResponse.getFirstHeader('Set-Cookie');       if (header != null) {           Global.cookie = header.getValue();       }
推荐信息