Help us understand the problem. What is going on with this article?

VolleyでのJSESSIONIDとAWSELB取得方法

More than 5 years have passed since last update.

概要

StickySessionを設定したAWSサーバとAndroidアプリとの通信時にAndroidアプリ側からJavaWebサーバのCookie「JSESSIONID」とAWSのELBのCookie「AWSELB」の内容を同時に取得する方法

詳細

サーバ側

サーバ側については以下のところを参照
Java/ServletとJSESSIONIDのURL管理
スティッキーセッションの作成

Androidアプリ側

Volley

Androidアプリで非同期通信を簡単に実現することの出来るネットワーク通信ライブラリ。
ネットワーク通信用ライブラリVolleyを使いこなす

取得方法

VolleyHttpRequest.java
public class VolleyHttpRequest
{
    private RequestQueue mQueue;

    public void request(Context context)
    {
        // Cookieの設定
        DefaultHttpClient client = new DefaultHttpClient();
        BasicClientCookie cookie = new BasicClientCookie(cookieName, cookieValue);
        cookie.setDomain("domain");
        cookie.setPath("/");
        cookie.setSecure(true);
        client.getCookieStore().addCookie(cookie);
        HttpStack httpStack = new HttpClientStack(client);

        // キューの作成
        mQueue = Volley.newRequestQueue(context, httpStack);

        // リクエストクラスの用意
        VolleyRequest volleyRequest = new VolleyRequest(Method.POST,
                                                        "リクエストURL",
                                                        "通信エラー時のリスナクラス",
                                                        client);

        // キューにリクエストを追加
        mQueue.add(volleyRequest);
    }
}
VolleyRequest.java
public abstract class VolleyRequest extends Request<JSONObject>
{
    private DefaultHttpClient mClient;

    public VolleyRequest(int method,
                         String url,
             ErrorListener errorListener,
             DefaultHttpClient client)
    {
        super(method, url, errorListener);
        mClient = client;
    }

    @Override
    protected Response<JSONObject> parseNetworkResponse(NetworkResponse response)
    {
        for(Cookie cookie : mClient.getCookieStore().getCookies())
        {
            if(cookie.getName().equals("JSESSIONID"))
            {
                // "JSESSIONID"取得
            }
            if(cookie.getName().equals("AWSELB"))
            {
                // "AWSELB"取得
            }
        }
    }
}

あとがき

本来(?)ならparseNetworkResponseで渡ってくるNetworkResponse.headersの中にCookie情報が入っているはずなのですが実際のところはJSESSIONIDしか取れませんでした。もしかしたらAWSの設定が悪いのかも。。。なんとも言えませんが。。。
とりあえず、上記の方法で両方のCookieを取得することが出来ます。

Why do not you register as a user and use Qiita more conveniently?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away