1

サーバーに対して 2 つの要求を行う必要があります。最初のリクエストでパラメーター userID = 1 を送信すると、サーバーは Cookie _session_ID を返します。ここで、パラメーターを指定せずに 2 番目の要求を送信すると、結果は 1 回目と同じになるはずです。ただし、Cookie は送信されません。なんで?

これは私のコードです:

public class Server
{
    static HttpClient   httpclient;
    static HttpPost     httppost;
    static String       JsonString;
    static HttpResponse response;
    static List <NameValuePair> nameValuePairs;
    static CookieStore cookieStore;
    static HttpContext localContext;
    static List<Cookie> cookies;
    static int cookieSize;

    public static void Clear()
    {
        try
        {
            cookieStore    = new BasicCookieStore();
            localContext   = new BasicHttpContext();    
            localContext.setAttribute(ClientContext.COOKIE_STORE, cookieStore);

            httpclient     = new DefaultHttpClient();
            httppost       = new HttpPost("http://mobile-app-storage.herokuapp.com/test");  
            nameValuePairs = new ArrayList <NameValuePair> (2);
            nameValuePairs.add(new BasicNameValuePair("userID",    "1"));
            httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
            response       = httpclient.execute(httppost, localContext);            
            JsonString = ReadFromServer(response.getEntity().getContent());

            Log.e("Request", "1");
            cookies    = cookieStore.getCookies();
            cookieSize = cookies.size();
            for (int i = 0; i < cookieSize; i++)
            {
                Log.v("Cookie "+ i, "name: "+cookies.get(i).toString());
            }

            // Second request without userID. Must be same result as first

            httpclient     = new DefaultHttpClient();
            httppost       = new HttpPost("http://mobile-app-storage.herokuapp.com/test");  
            nameValuePairs.clear();
            httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
            response       = httpclient.execute(httppost);          
            JsonString = ReadFromServer(response.getEntity().getContent());

            Log.e("Request", "2");
            cookies    = cookieStore.getCookies();
            cookieSize = cookies.size();
            for (int i = 0; i < cookieSize; i++)
            {
                Log.v("Cookie "+ i, "name: "+cookies.get(i).toString());
            }
        }
        catch (Throwable e) {Log_.Error(e);}
    }
}
4

1 に答える 1