【问题标题】:Preserving session in HttpClient在 HttpClient 中保留会话
【发布时间】:2013-04-21 20:08:29
【问题描述】:

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

第一次请求后,在它返回的众多 cookie 中,这是会话 ID cookie:

name: ASPSESSIONIDSCSCSBCS
value: GBAJALJBOGKBFLAELPNKEDOE

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

name: ASPSESSIONIDSCSCSBCS
value: GBAJALJBOGKBFLAELPNKEDOE

name: ASPSESSIONIDSCSCSBCS
value: MBAJALJBDBOKPEHNCDDFOCBC

我假设因为在第二个请求期间会话 ID 不同,它会忽略具有第一个会话 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);

【问题讨论】:

标签: java android cookies web-scraping httpclient


【解决方案1】:

我认为你应该用 a

覆盖 cookieStore
 cookieStore = (BasicCookieStore) httpClient.getCookieStore();

只有在登录成功后..
之后,我认为您应该使用新的 cookieStore 再次设置 Context..
告诉我!

【讨论】:

    猜你喜欢
    • 2011-12-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-11-07
    • 2017-09-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多