gpt4 book ai didi

java - 请求不使用 PersistentCookieStore 中保存的 cookie

转载 作者:可可西里 更新时间:2023-11-01 17:10:56 25 4
gpt4 key购买 nike

我在声明 cookie 存储时修复了我的应用程序中的崩溃和错误,但它没有保存 cookie 或在其他位置出现问题。

起初我调用这两行:

AsyncHttpClient client = new AsyncHttpClient();
PersistentCookieStore myCookieStore;

然后我有一个 POST:

public void postRequestLogin(String url, RequestParams params) {
myCookieStore = new PersistentCookieStore(this);
client.post(url, params, new AsyncHttpResponseHandler() {
@Override
public void onSuccess(String response) {
client.setCookieStore(myCookieStore);
System.out.println(response);

if(response.contains("Login successful!")) {
TextView lblStatus = (TextView)findViewById(R.id.lblStatus);
lblStatus.setText("Login successful!");
getRequest("url");
} else {
TextView lblStatus = (TextView)findViewById(R.id.lblStatus);
lblStatus.setText("Login failed!");
TextView source = (TextView)findViewById(R.id.response_request);
source.setText(response);
}
}
});

}

然后它应该保存 Logincookies 并将其用于 GET 请求:

public void getRequest(String url) {
myCookieStore = new PersistentCookieStore(this);
client.get(url, new AsyncHttpResponseHandler() {
@Override
public void onSuccess(String response) {
client.setCookieStore(myCookieStore);
System.out.println(response);
TextView responseview = (TextView) findViewById(R.id.response_request);
responseview.setText(response);
}
});
}

但它不使用 cookie。当我执行 GET 请求时,我已经注销了。

编辑:我忘了说我使用了本教程中的库:http://loopj.com/android-async-http/

最佳答案

我认为问题在于您在请求完成后设置了 cookie 存储(在 onSuccess 方法中)。在发出该请求之前尝试设置它:

myCookieStore = new PersistentCookieStore(this);
client.setCookieStore(myCookieStore);
client.post(url, params, new AsyncHttpResponseHandler() {

您还针对每个请求创建了一个新的 cookie 存储。如果您执行多个请求会怎样?它将创建一个新的 cookie 存储并使用它(新的 cookie 存储不会有您的 cookie)。尝试将这部分代码移到您的构造函数中:

myCookieStore = new PersistentCookieStore(this);
client.setCookieStore(myCookieStore);

然后将其从其他函数中移除。

关于java - 请求不使用 PersistentCookieStore 中保存的 cookie,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15668186/

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