LoginSignup
5
5

More than 5 years have passed since last update.

VolleyでのJSESSIONIDとAWSELB取得方法

Last updated at Posted at 2014-07-07

概要

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を取得することが出来ます。

5
5
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
5
5