2010-10-15 85 views
2

我正在开发一个使用android 2.1的应用程序。会话cookie在模拟器上工作,但不是真实的设备

我使用会话cookie登录到RESTful Web服务时遇到问题。该代码在模拟器上正常工作,但是当我在HTC Magic上运行它时,Cookie逻辑不起作用。我已经确认,通过列出这些魔术曲是通过标题接收cookie(请参阅附件)。任何人都可以说为什么cookie商店即使他们在标题中也是空的吗?

public HttpProvider() { 
    Scheme http = new Scheme("http", new PlainSocketFactory(), 80); 
    SchemeRegistry registry = new SchemeRegistry(); 
    registry.register(http); 

    client = new DefaultHttpClient(new ThreadSafeClientConnManager((new BasicHttpParams()), registry), new BasicHttpParams()); 
    } 

    public void get(Request request) { 
    try { 
     HttpGet get = new HttpGet(request.url); 
     HttpResponse response = client.execute(get); 
     debugListHeaders(response, request.ticket); 
     debugListCookies(); 
    } 
    } 

    void debugListHeaders(HttpResponse response, int ticket) { 
    Header[] headers = response.getAllHeaders(); 
    Log.d(LOG, "Printing all headers" + " (" + ticket + ")"); 
    for (Header header : headers) { 
     Log.d(LOG, "Header name: " + header.getName() + ", value: " + header.getValue() + " (" + ticket + ")"); 
    } 
    Log.d(LOG, "All headers have been printed" + " (" + ticket + ")"); 
    } 

    void debugListCookies() { 
    Log.d(LOG, "List cookies for connection"); 
    for (Cookie cookie : client.getCookieStore().getCookies()) { 
     Log.d(LOG, 
     "Domain: " + cookie.getDomain() + 
     " Name: " + cookie.getName() + 
     " Path: " + cookie.getPath() + 
     " Value: " + cookie.getValue() + 
     " Expires: " + cookie.getExpiryDate().toString() + 
     " IsExpired: " + (cookie.isExpired(new Date()) ? "true" : "false")); 
    } 
    Log.d(LOG, "All cookies have been listed"); 
    } 

Thru the emulator: 
<snipped header list as it's obviously working> 
D/SessionProvider( 257): List cookies for connection 
D/SessionProvider( 257): Domain:<snip> Name: S Path:/Value: 75XGSMR3BLLGYM0J Expires: Tue Oct 12 20:24:09 America/Barbados 2010 
IsExpired: false 
D/SessionProvider( 257): Domain: <snip> Name: lang Path:/Value: en Expires: Tue Oct 12 20:24:09 America/Barbados 2010 IsExpired: false 
D/SessionProvider( 257): All cookies have been listed 


Thru HTC Magic: 
D/HttpProvider( 983): Printing all headers (-192275970) 
D/HttpProvider( 983): Header name: Date, value: Thu, 14 Oct 2010 22:44:38 GMT (-192275970) 
D/HttpProvider( 983): Header name: Server, value: Apache/2.2.11 (Ubuntu) PHP/5.2.6-3ubuntu4.5 with Suhosin-Patch mod_perl/2.0.4 Perl/v5.10.0 (-192275970) 
D/HttpProvider( 983): Header name: Set-Cookie, value: S=S41GM85A675Z8YQU; path=/; expires=Thu, 14-Oct-2010 23:14:38 GMT (-192275970) 
D/HttpProvider( 983): Header name: Set-Cookie, value: U=nibor.yarrum%40gmail.com; path=/; expires=Thu, 14-Oct-2010 23:14:38 GMT (-192275970) 
D/HttpProvider( 983): Header name: Set-Cookie, value: lang=en; path=/; expires=Thu, 14-Oct-2010 23:14:38 GMT (-192275970) 
D/HttpProvider( 983): Header name: Vary, value: Accept-Encoding (-192275970) 
D/HttpProvider( 983): Header name: Keep-Alive, value: timeout=15, max=100 (-192275970) 
D/HttpProvider( 983): Header name: Connection, value: Keep-Alive (-192275970) 
D/HttpProvider( 983): Header name: Transfer-Encoding, value: chunked (-192275970) 
D/HttpProvider( 983): Header name: Content-Type, value: text/html (-192275970) 
D/HttpProvider( 983): All headers have been printed (-192275970) 
I/HttpProvider( 983): Request completed (-192275970) 
D/SessionProvider( 983): List cookies for connection 
D/SessionProvider( 983): All cookies have been listed 
+0

更新 - 我已经试过这两个更多的手机,他们都很好地工作。一部电话不处理cookie。叹。 – mvsjes2 2010-10-16 19:29:32

回答

2

那么,这原来是一个在apache代码中的狩猎探险队。它被归结为饼干已经过期,所以饼干店就把它们扔掉了,没有任何解释。原来手机没有在设置中选中“使用网络日期/时间”。只要我这样做,应用程序运行良好。这仍然令人费解 - 当地时间设置正确(2分钟内)。

从org.apache.http.impl.client.BasicCookieStore /的addCookie:

94 if (!cookie.isExpired(new Date())) { 
95  cookies.add(cookie); 
+0

在PHP中,我没有使用'$ _COOKIE ['cookieName']',而是使用了'setCookie(“cookieName”,$ value,$ time()+ 3600)',它可以工作! – Eamorr 2011-07-17 10:16:02

+0

@ mvsjes2嘿,我有同样的问题,并尝试实现您的答案,但尚未解决的问题。 – 2013-09-13 12:12:25

相关问题