get Session From HTTP Header - Android Network

Android examples for Network:HTTP Get

Description

get Session From HTTP Header

Demo Code


//package com.java2s;

import java.util.Map;

public class Main {
    public static final String HEADER_COOKIE_KEY = "Set-Cookie";
    public static final String COOKIE_KEY = "JSESSIONID";

    public static String getSessionFromHeader(Map<String, String> headers) {
        String cookieValue = null;
        if (headers != null) {
            for (String key : headers.keySet()) {
                String value = headers.get(key);
                if (HEADER_COOKIE_KEY.equals(key)) {
                    cookieValue = getCookie(value);
                    break;
                }// w  w w .j  a v  a2 s  . c o  m
            }
        }
        return cookieValue;
    }

    private static String getCookie(String headerString) {
        String cookieValue = null;
        if (headerString != null) {
            int keyIndex = headerString.indexOf(COOKIE_KEY);
            if (keyIndex >= 0) {
                int endIndex = headerString.indexOf(";", keyIndex);
                if (endIndex > keyIndex) {
                    cookieValue = headerString.substring(keyIndex
                            + COOKIE_KEY.length() + 1, endIndex);
                }
            }
        }
        return cookieValue;
    }
}

Related Tutorials