会话未在httpUrlConn调用之间维护

时间:2015-11-10 07:40:16

标签: java android session httpurlconnection

我有以下方法:

@lastDate

我将用户名和密码发布到服务器,服务器创建会话," Set-Cookie"保存到CookieManager中,如:

  public static String postJSONObject(String myurl, JSONObject parameters, Context ctx) {
        HttpURLConnection conn = null;
        try {
            StringBuffer response = null;
            URL url = new URL(myurl);
            conn = (HttpURLConnection) url.openConnection();
            conn.setReadTimeout(10000);
            conn.setConnectTimeout(15000);
            conn.setRequestProperty("Content-Type", "application/json");
            conn.setDoOutput(true);

            conn.setRequestMethod("POST");
            OutputStream out = new BufferedOutputStream(conn.getOutputStream());

            BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(out, "UTF-8"));
            writer.write(parameters.toString());
            Log.d("Parameters to string", "params.tostring" + parameters.toString());
            writer.close();
            out.close();
            int responseCode = conn.getResponseCode();
            System.out.println("responseCode" + responseCode);

            switch (responseCode) {
                case 200:

                    BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
                    String inputLine;
                    response = new StringBuffer();
                    while ((inputLine = in.readLine()) != null) {
                        response.append(inputLine);
                    }
                    in.close();

                    List cookieList = conn.getHeaderFields().get("Set-Cookie");
                    if (cookieList != null) {
                        for (Object cookieTemp : cookieList) {
                            CookieManager.getInstance().setCookie(conn.getURL().toString(), cookieTemp.toString());
                        }
                    }
                    String cookie = CookieManager.getInstance().getCookie(new URL("https://exaperth.exacrm.com").getHost());
                    System.out.println("Cookie after adding " + cookie);
                    return response.toString();
            }
        } catch (IOException ex) {
            ex.printStackTrace();
        } finally {
            if (conn != null) {
                try {
                    conn.disconnect();
                } catch (Exception ex) {
                    ex.printStackTrace();
                }
            }
        }
        return null;
    }

在下面的第二种方法中:

PHPSESSID=a3d2367f8a5a3221e9bad1a91a34fd55; ci_session=a%3A4%3A%7Bs%3A10%3A%22session_id%22%3Bs%3A32%3A%221381c152699fb61d04663c9b854ecdd7%22%3Bs%3A10%3A%22ip_address%22%3Bs%3A14%3A%22182.59.245.196%22%3Bs%3A10%3A%22user_agent%22%3Bs%3A60%3A%22Dalvik%2F2.1.0+%28Linux%3B+U%3B+Android+5.1.1%3B+Nexus+5+Build%2FLMY48B%29%22%3Bs%3A13%3A%22last_activity%22%3Bi%3A1447140920%3B%7Ddd2c014724b9ca061b50774f1fea235d

我使用相同的cookie,但不知何故,会话无效,我得到了登录页面。我错过了什么?

0 个答案:

没有答案