2013-04-27 124 views
3

我正在嘗試登錄到wesbite。在我的第一個請求中,我使用用戶名和密碼在登錄頁面上執行了POST併成功登錄。使用同一個HttpClient實例,我在網站上的另一個頁面上做了第二次請求,這次是GET請求,但是這一次,返回的頁面沒有登錄。我認爲這是由於第二個請求正在一個新的會議中完成。在HttpClient中保留會話

第一個請求後,它會返回許多餅乾之中,這是會話ID的Cookie:

name: ASPSESSIONIDSCSCSBCS 
value: GBAJALJBOGKBFLAELPNKEDOE 

而第二個請求後,許多其他餅乾中,我有兩個不同的會話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); 

回答

0

我想你應該只在登錄成功後,用

cookieStore = (BasicCookieStore) httpClient.getCookieStore(); 

覆蓋的CookieStore ..
之後,我想你應該重新設置與新的CookieStore上下文..
讓我知道!