httpmime-session 会话保持

sesion在浏览器和web服务器直接是通过一个叫做name为sessionid的cookie来传递的,所以只要在每次数据请求时保持sessionid是同一个不变就可以用到web的session了,做法是第一次数据请求时就获取sessionid的值并保存在一个静态变量中,然后在第二次请求数据的时候要将这个sessionid一并放在Cookie中发给服务器,服务器则是通过这个sessionid来识别究竟是那个客户端在请求数据的,

在php中这个sessionid的名字叫做PHPSESSID 

在Java中这个sessionid的名字叫做JSESSIONID

 1 /**
 2      * post请求
 3      * 
 4      * @param uri
 5      * @param params
 6      * @param handler
 7      * @param context
 8      */
 9     public static void doPost(final String uri,
10             final HashMap<String, String> params, final Handler handler,
11             final Context context) {
12         final Message msg = handler.obtainMessage();
13         final ProgressDialog progress = ProgressDialog
14                 .show(context, null, "加载");
15         new Thread() {
16             public void run() {
17                 try {
18                     HttpClient client = new DefaultHttpClient();
19                     HttpPost httppost = new HttpPost(uri.trim());
20                     MultipartEntity reqEntity = new MultipartEntity();
21                     if (params != null) {
22                         Set<String> param = params.keySet();
23                         StringBuilder sb = new StringBuilder();
24                         if (params != null && !params.isEmpty()) {
25                             for (String par : param) {
26                                 String value = params.get(par);
27                                 if (null == value) {
28                                     return;
29                                 }
30                                 reqEntity.addPart(par, new StringBody(value,
31                                         Charset.forName("UTF-8")));
32                             }
33                         }
34                     }
35                     httppost.setEntity(reqEntity);
36                     Log.i("info", "设置:" + JSESSIONID);
37                     if (null != JSESSIONID) {
38                         httppost.setHeader("Cookie", "JSESSIONID=" + JSESSIONID);
39                     }
40                     HttpResponse response = client.execute(httppost);
41                     HttpEntity resEntity = response.getEntity();
42                     Header[] hs = response.getAllHeaders();
43                     for (Header h : hs) {
44                         Log.i("info",
45                                 "header:" + h.getName() + "," + h.getValue());
46                     }
47                     if (resEntity != null) {
48                         CookieStore mCookieStore = ((AbstractHttpClient) client)
49                                 .getCookieStore();
50                         List<Cookie> cookies = mCookieStore.getCookies();
51                         for (int i = 0; i < cookies.size(); i++) {
52                             // 这里是读取Cookie['PHPSESSID']的值存在静态变量中,保证每次都是同一个值
53                             if ("JSESSIONID".equals(cookies.get(i).getName())) {
54                                 JSESSIONID = cookies.get(i).getValue();
55                                 break;
56                             }
57                         }
58                         Log.i("info", "取到:" + JSESSIONID);
59 
60                         InputStream in = resEntity.getContent();
61                         ByteArrayOutputStream baos = new ByteArrayOutputStream();
62                         int i = -1;
63                         while ((i = in.read()) != -1) {
64                             baos.write(i);
65                         }
66                         msg.what = 1;
67                         msg.obj = new String(baos.toByteArray(), "utf-8");
68                     } else {
69                         msg.what = 0;
70                     }
71                 } catch (IOException e) {
72                     msg.what = 0;
73                     msg.obj = "";
74                     e.printStackTrace();
75                 }
76                 handler.sendMessage(msg);
77                 if (null != progress) {
78                     progress.dismiss();
79                 }
80             };
81         }.start();
82     }
83 
84     public static String JSESSIONID = null;

其实web的原理都是一样的,基于http协议的,那么如果网站不是java,php做的话,那个叫做Sessionid的Cookie可能叫做别的了 ,而是叫做别的名字了,这个可能要具体情况去查了。

其实不只是Android程序,其他任何程序需要这么用的时候只需要在http协议请求header里头加上发送相应的SessionId就可以了。刚刚这种方法是可以帮助理解sessionid的,其实还有一种方法如果更通用的话,就可以将刚刚所有的Cookie每次都发回到服务器端,也就可以解决session保持的问题了,只是这样可能会稍微大些网络流量开销而已。

来自:http://l62s.iteye.com/blog/1663113

原文地址:https://www.cnblogs.com/mjorcen/p/3620455.html