// Use this for initialization
    void Start()
    {
        Debug.Log("start");
        SpringBootClient.Instance.baseUrl = "http://localhost:8080";
        StartCoroutine(SpringBootClient.Instance.LoginByFormPost("admin", "admin", data =>
        {
            if (data.authenticated)
            {
                Debug.Log("Successfully authenticated!");
                Debug.Log("JSESSIONID: " + SpringBootClient.Instance.sessionId);
                Debug.Log("CSRF: " + SpringBootClient.Instance._csrf);

                StartCoroutine(SpringBootClient.Instance.GetSecured("http://localhost:8080/users/get-account", json =>
                {
                    Debug.Log("account: " + json);
                }));

                SpringIdentity si = new SpringIdentity();
                si.username       = "******";
                StartCoroutine(SpringBootClient.Instance.PostJsonSecured("http://localhost:8080/users/get-account-by-username", si, json =>
                {
                    Debug.Log("account: " + json);
                }));
            }
        }));
    }
    public IEnumerator LoginByFormPost(string username, string password, LoginCallback callback)
    {
        WWW cases = new WWW(baseUrl + "/erp/login-api-form-post");

        yield return(cases);

        string json = cases.text;

        /*
         * foreach(string header_name in cases.responseHeaders.Keys)
         * {
         *  Debug.Log("GET HEADER[" + header_name + "] = " + cases.responseHeaders[header_name]);
         * }*/

        Debug.Log(json);

        Dictionary <string, string> result = JsonConvert.DeserializeObject <Dictionary <string, string> >(json);

        _csrf = result["_csrf.token"];

        Debug.Log("_csrf: " + _csrf);



        string data = "username="******"&password="******"send: " + data);

        byte[] postData = System.Text.Encoding.UTF8.GetBytes(data);

        Dictionary <string, string> headers = new Dictionary <string, string>();

        headers.Add("Content-Type", "application/x-www-form-urlencoded");
        headers.Add("_csrf", _csrf);
        headers.Add("Cookie", "XSRF-TOKEN=" + _csrf);
        headers.Add("X-XSRF-TOKEN", _csrf);


        //Now we call a new WWW request
        WWW www = new WWW(baseUrl + "/erp/login-api-form-post", postData, headers);

        yield return(www);

        json = www.text;

        string set_cookie = null;

        foreach (string header_name in www.responseHeaders.Keys)
        {
            //Debug.Log("POST HEADER[" + header_name + "] = " + www.responseHeaders[header_name]);
            if (header_name.ToLower() == "set-cookie")
            {
                set_cookie = www.responseHeaders[header_name];
                break;
            }
        }


        Debug.Log("POST Set-Cookie: " + set_cookie);

        if (set_cookie != null)
        {
            string[] parts = set_cookie.Split(';');
            foreach (string part in parts)
            {
                string[] pair = part.Trim().Split('=');
                if (pair.Length == 2)
                {
                    string name = pair[0].Trim();
                    if (name == "JSESSIONID")
                    {
                        sessionId = pair[1].Trim();
                    }
                }
            }
        }

        Debug.Log("JSESSIONID: " + sessionId);



        Debug.Log(json);

        SpringIdentity si = JsonConvert.DeserializeObject <SpringIdentity>(json);

        callback(si);
    }