gpt4 book ai didi

android - session cookie 在模拟器上有效,但在真实设备上无效

转载 作者:太空狗 更新时间:2023-10-29 13:42:35 26 4
gpt4 key购买 nike

我正在使用 android 2.1 开发应用。

我在使用 session cookie 登录 RESTful 网络服务时遇到问题。该代码在模拟器上运行良好,但当我在我的 HTC Magic 上运行它时,cookie 逻辑不起作用。我已经通过列出它们来确认魔术正在接收 header 中的 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

最佳答案

嗯,这原来是apache代码中的狩猎探险。归根结底是 cookies 已经过期了,所以 cookiestore 没有任何解释就把它们扔掉了。原来手机没有在设置上选中“使用网络日期/时间”。一旦我这样做,该应用程序就可以正常工作。不过,这仍然令人费解 - 本地时间设置正确(2 分钟以内)。

来自 org.apache.http.impl.client.BasicCookieStore/addCookie:

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

关于android - session cookie 在模拟器上有效,但在真实设备上无效,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3938714/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com