0

セッションに関してログインしてCookieを取得することができました。しかし、新しいリクエストを作成しようとすると、ログイン情報が失われているようです (HTML データは両方のリクエストで同じです。2 番目のリクエストでは、ユーザー名とその他のデータが提供されるはずです)。

新しいリクエストを送信する前に、次のように Cookie を設定します (DefaultHttpClient インスタンスは同じです)。

    List<Cookie> cookies = httpclient.getCookieStore().getCookies();

    if(cookies != null)
    {
        for(Cookie cookie : cookies)
        {
            String cookieString = cookie.getName() + "=" + cookie.getValue() + "; domain=" + cookie.getDomain();  
            httppost.addHeader("Cookie",cookie.getName() + "=" + cookie.getValue() + ";");
            System.out.println(cookieString);
        }
    }  

    try
    {
        //System.out.println(httpclient.getCookieStore().getCookies());
        response = (BasicHttpResponse) httpclient.execute(httppost,localContext);
    }

Cookie 情報を確認したところ、上記の for ループで設定した 2 つの異なる「Cookie インスタンス」(2 つのセッション ID) が返されるようです。しかし、それでもうまくいかないようです。何が問題になる可能性がありますか?

助けてくれてありがとう!

4

1 に答える 1

0
// Create a static instance of cookie store globally 

     cookieStore = new BasicCookieStore();

 // Create local HTTP context

    HttpContext localContext = new BasicHttpContext();

// Bind custom cookie store to the local context
localContext.setAttribute(ClientContext.COOKIE_STORE, cookieStore);
//execute your connection with context
  HttpResponse response = http.execute(post,localContext);


And then whenever you connect use that static cookie instance to connect
HttpContext localContext = new BasicHttpContext();
// Bind custom cookie store to the local context
localContext.setAttribute(ClientContext.COOKIE_STORE, StaticInstance(cookieStore));
//and as usual
response = http.execute(post,localContext);
于 2013-05-23T06:10:00.847 に答える