Session与Cookie

Cookie和Session都为了用来保存状态信息,都是保存客户端状态的机制,它们都是为了解决HTTP无状态的问题而所做的努力。

Session可以用Cookie来实现,也可以用URL回写的机制来实现。

Cookie和Session有以下明显的不同点:

1)Cookie将状态保存在客户端,Session将状态保存在服务器端;

2)Cookies是服务器在本地机器上存储的小段文本并随每一个请求发送至同一个服务器。网络服务器用HTTP头向客户端发送cookies,在客户终端,浏览器解析这些cookies并将它们保存为一个本地文件,它会自动将同一服务器的任何请求缚上这些cookies。

3)Session是针对每一个用户的,变量的值保存在服务器上,用一个sessionID来区分是不同用户session变量,这个值是通过用户的浏览器在访问的时候返回给服务器,当客户禁用cookie时,这个值也可能设置为由get来返回给服务器;

4)就安全性来说:当你访问一个使用session 的站点,同时在自己机器上建立一个cookie,建议在服务器端的SESSION机制更安全些.因为它不会任意读取客户存储的信息。

Session机制

Session机制是一种服务器端的机制,服务器使用一种类似于散列表的结构(也可能就是使用散列表)来保存信息。

当程序需要为某个客户端的请求创建一个session的时候,服务器首先检查这个客户端的请求里是否已包含了一个session标识 - 称为 session id,如果已包含一个session id则说明以前已经为此客户端创建过session,服务器就按照session id把这个 session检索出来使用(如果检索不到,可能会新建一个),如果客户端请求不包含session id,则为此客户端创建一个session并且生成一个与此session相关联的session id,session id的值应该是一个既不会重复,又不容易被找到规律以仿造的字符串,这个 session id将被在本次响应中返回给客户端保存。

Session的实现方式

1)使用Cookie来实现

服务器给每个Session分配一个唯一的JSESSIONID,并通过Cookie发送给客户端。

当客户端发起新的请求的时候,将在Cookie头中携带这个JSESSIONID。这样服务器能够找到这个客户端对应的Session。

2)使用URL回显来实现

URL回写是指服务器在发送给浏览器页面的所有链接中都携带JSESSIONID的参数,这样客户端点击任何一个链接都会把JSESSIONID带给服务器。
如果直接在浏览器中输入url来请求资源,Session是匹配不到的。

Tomcat对 Session的实现,是一开始同时使用Cookie和URL回写机制,如果发现客户端支持Cookie,就继续使用Cookie,停止使用URL回写。如果发现Cookie被禁用,就一直使用URL回写。jsp开发处理到Session的时候,对页面中的链接记得使用 response.encodeURL() 。

手机端与服务器交互没有实现在同一session下?

原因很简单,就是因为android手机端在访问web服务器时,没有给http请求头部设置sessionID,而使用web浏览器作为客户端访问服务器时,在客户端每次发起请求的时候,都会将交互中的sessionID:JSESSIONID设置在Cookie头中携带过去,服务器根据这个sessionID获取对应的Session,而不是重新创建一个新Session(除了这个Session失效)。

Code(1) HttpURLConnection实现

URL url = new URL(requrl);HttpURLConnection con= (HttpURLConnection) url.openConnection(); // 取得sessionid. String cookieval = con.getHeaderField("set-cookie"); String sessionid; if(cookieval != null) {     sessionid = cookieval.substring(0, cookieval.indexOf(";")); }//sessionid值格式:JSESSIONID=AD5F5C9EEB16C71EC3725DBF209F6178,是键值对,不是单指值发送设置cookie: URL url = new URL(requrl);HttpURLConnectioncon= (HttpURLConnection) url.openConnection(); if(sessionid != null) {     con.setRequestProperty("cookie", sessionid); }

Code(2) HttpClient 单例模式实现

只要存在一个HttpClient对象就可以了,这个HttpClient对象中就包含得有Cookie信息。

我在工程中是使用的单例模式实现的:

public class Client {    private static HttpClient instance = null;    private Client() {    }    public static HttpClient getInstance() {        if (instance == null) {            return instance = new DefaultHttpClient();        } else {            return instance;        }    }}
class myThread extends Thread {        @Override        public void run() {            try {                HttpClient client = Client.getInstance();                            String path = "http://192.168.1.4/zxx/test.php";                HttpPost httpPost = new HttpPost(path);                List<NameValuePair> param = new ArrayList<NameValuePair>();                param.add(new BasicNameValuePair("phonenumber", "18200000000"));                httpPost.setEntity(new UrlEncodedFormEntity(param, "utf-8"));                HttpResponse response = client.execute(httpPost);                int code = response.getStatusLine().getStatusCode();                if (code == 200) {                    InputStream is = response.getEntity().getContent();                    ByteArrayOutputStream baos = new ByteArrayOutputStream();                    int len = 0;                    byte[] buffer = new byte[1024];                    while ((len = is.read(buffer)) != -1) {                        baos.write(buffer, 0, len);                    }                    is.close();                    baos.close();                    byte[] result = baos.toByteArray();                    String SysOut = new String(result, "utf-8");                    System.out.println(SysOut);                } else {                    System.out.println("code----------->" + code + "");                }            } catch (Exception e) {                e.printStackTrace();            }finally            {                            }            super.run();        }    }        class myThread2 extends Thread {        @Override        public void run() {            try {                HttpClient client = Client.getInstance();                                String path = "http://192.168.1.4/zxx/test1.php";                HttpPost httpPost = new HttpPost(path);                List<NameValuePair> param = new ArrayList<NameValuePair>();                param.add(new BasicNameValuePair("phonenumber", "18200000000"));                                HttpResponse response = client.execute(httpPost);                int code = response.getStatusLine().getStatusCode();                if (code == 200) {                    InputStream is = response.getEntity().getContent();                    ByteArrayOutputStream baos = new ByteArrayOutputStream();                    int len = 0;                    byte[] buffer = new byte[1024];                    while ((len = is.read(buffer)) != -1) {                        baos.write(buffer, 0, len);                    }                    is.close();                    baos.close();                    byte[] result = baos.toByteArray();                    String SysOut = new String(result, "utf-8");                    System.out.println(SysOut);                } else {                    System.out.println("code----------->" + code + "");                }            } catch (Exception e) {                e.printStackTrace();            }            super.run();        }    }

两个线程中就可以使用同一个cookie访问了。

Code(3) SeesionId Url传递实现

通过将SessionId的值通过url传递到想继续在同一会话中访问的页面。

php代码:test2.php

<?phpsession_start();header("Content-type:text/html;charset=utf-8");if(isset($_POST['phonenumber'])){    $phone = $_POST['phonenumber'];    if(!isset($_SESSION['phone']))    {        $time=time()+60*10*10;//100分钟        $_SESSION['phone'] = $phone;        echo session_id();    }    else        echo "POST phone already exist";}if(isset($_GET['phonenumber'])){    $phone = $_GET['phonenumber'];    if(!isset($_SESSION['phone']))    {        $time=time()+60*10*10;//100分钟        $_SESSION['phone'] = $phone;            echo session_id();    }    else        echo "GET phone already exist";}    ?>

test3.php

Session_id($_GET['id']);session_start();header("Content-type:text/html;charset=utf-8");if(isset($_POST['phonenumber'])){    $phone = $_POST['phonenumber'];    echo "phone-->".$phone."<br />";    echo "_SESSION[phone]-->".$_SESSION['phone']."<br />";    echo "SESSIONid--->".session_id();    if($_SESSION['phone'] == $phone)        echo "POST 验证 OK";    else        echo "POST 验证 BAD";}if(isset($_GET['phonenumber'])){    $phone = $_GET['phonenumber'];    echo "phone-->".$phone."<br />";    echo "_SESSION[phone]-->".$_SESSION['phone']."<br />";    echo "session_id--->".session_id();    if($_SESSION['phone'] == $phone)        echo "GET 验证 OK";    else        echo "GET 验证 BAD";}

Android上的代码(这里我们分别new两个HttpClient做测试):

class myThread extends Thread {        @Override        public void run() {            try {                client = new DefaultHttpClient();                            String path = "http://192.168.1.4/zxx/test.php";                HttpPost httpPost = new HttpPost(path);                List<NameValuePair> param = new ArrayList<NameValuePair>();                param.add(new BasicNameValuePair("phonenumber", "18200000000"));                httpPost.setEntity(new UrlEncodedFormEntity(param, "utf-8"));                HttpResponse response = client.execute(httpPost);                int code = response.getStatusLine().getStatusCode();                if (code == 200) {                    InputStream is = response.getEntity().getContent();                    ByteArrayOutputStream baos = new ByteArrayOutputStream();                    int len = 0;                    byte[] buffer = new byte[1024];                    while ((len = is.read(buffer)) != -1) {                        baos.write(buffer, 0, len);                    }                    is.close();                    baos.close();                    byte[] result = baos.toByteArray();                    String SysOut = new String(result, "utf-8");                    System.out.println(SysOut);                    seesionId = SysOut;                } else {                    System.out.println("code----------->" + code + "");                }            } catch (Exception e) {                e.printStackTrace();            }finally            {                            }            super.run();        }    }        class myThread2 extends Thread {        @Override        public void run() {            try {                client = new DefaultHttpClient();                                String path = "http://192.168.1.4/zxx/test1.php?id="+seesionId;                HttpPost httpPost = new HttpPost(path);                List<NameValuePair> param = new ArrayList<NameValuePair>();                param.add(new BasicNameValuePair("phonenumber", "18200000000"));                httpPost.setEntity(new UrlEncodedFormEntity(param, "utf-8"));                HttpResponse response = client.execute(httpPost);                int code = response.getStatusLine().getStatusCode();                if (code == 200) {                    InputStream is = response.getEntity().getContent();                    ByteArrayOutputStream baos = new ByteArrayOutputStream();                    int len = 0;                    byte[] buffer = new byte[1024];                    while ((len = is.read(buffer)) != -1) {                        baos.write(buffer, 0, len);                    }                    is.close();                    baos.close();                    byte[] result = baos.toByteArray();                    String SysOut = new String(result, "utf-8");                    System.out.println(SysOut);                } else {                    System.out.println("code----------->" + code + "");                }            } catch (Exception e) {                e.printStackTrace();            }            super.run();        }    }

Session_id($_GET['id']); 这句话很重要。

我是天王盖地虎的分割线

更多相关文章

  1. 万字长文带你了解最常用的开源 Squid 代理服务器
  2. Nginx系列教程(一)| 手把手教你在Linux环境下搭建Nginx服务
  3. Nginx系列教程(二)| 一文带你读懂Nginx的正向与反向代理
  4. Nginx系列教程(三)| 一文带你读懂Nginx的负载均衡
  5. RHEL 6 下 DHCP+TFTP+FTP+PXE+Kickstart 实现无人值守安装
  6. Android(安卓)说说网络编程
  7. Android开发之LisitView的图文并排效果实现(源代码分享)
  8. HttpURLConnection和HttpClient
  9. Android下的RTSP客户端搭建

随机推荐

  1. 从罗马帝国精英军团/秦帝国军制谈iOS/And
  2. android开发动态页面时常用的LayoutInfla
  3. 第一行代码 学习笔记 详解广播机制
  4. [转载]Android自定义UI组件
  5. Android多语言切换(兼容安卓9、10)
  6. Android各个版本的新特性
  7. Android的log机制小结
  8. Android锁屏实现与总结
  9. Android(安卓)AsynTask 与Thread 或线程
  10. 【转】android 内存泄漏相关收藏博客。