gpt4 book ai didi

java - 在 HttpClient 中保留 session

转载 作者:太空狗 更新时间:2023-10-29 15:11:07 24 4
gpt4 key购买 nike

我正在尝试登录网站。在我的第一个请求中,我使用用户名和密码在登录页面上执行 POST 并成功登录。使用相同的 HttpClient 实例,我在网站的不同页面上进行了第二次请求,这次是 GET 请求,但是这次返回的页面未登录。我认为这是由于第二次请求正在新 session 中完成。

在第一个请求之后,在它返回的许多 cookie 中,这是 session ID cookie:

name: ASPSESSIONIDSCSCSBCS
value: GBAJALJBOGKBFLAELPNKEDOE

在第二次请求之后,在许多其他 cookie 中,我有两个不同的 session ID cookie:

name: ASPSESSIONIDSCSCSBCS
value: GBAJALJBOGKBFLAELPNKEDOE

name: ASPSESSIONIDSCSCSBCS
value: MBAJALJBDBOKPEHNCDDFOCBC

我假设因为在第二次请求期间 session ID 不同,它会忽略具有第一个 session ID 的 cookie。

我该如何解决这个问题?

编辑:这是我的代码

public HttpClient httpclient = new DefaultHttpClient();
public CookieStore cookieStore = new BasicCookieStore();
public HttpContext localContext = new BasicHttpContext();
localContext.setAttribute(ClientContext.COOKIE_STORE, cookieStore);

//The first request
HttpPost httppost = new HttpPost("http://www.deeproute.com/deeproute/default.asp");
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("cookieexists","false"));
nameValuePairs.add(new BasicNameValuePair("name", mUser));
nameValuePairs.add(new BasicNameValuePair("password", mPassword));
nameValuePairs.add(new BasicNameValuePair("subbera", "Login"));

httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
res = httpclient.execute(httppost, localContext);

//The second request
HttpGet rosterGet = new HttpGet("http://deeproute.com/deeproute/?sel=rosterlook&myleagueno=6&myteamno=12");
res = httpclient.execute(rosterGet, localContext);

最佳答案

我认为你应该用 覆盖cookieStore

 cookieStore = (BasicCookieStore) httpClient.getCookieStore();

只有在登录成功之后..
在那之后我认为你应该用新的 cookieStore 重新设置 Context..
让我知道!

关于java - 在 HttpClient 中保留 session ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16256961/

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