2012-01-30 49 views
0

我正在開發一個使用android 2.2的應用程序。我不知道如何管理服務器和移動應用程序之間的打開的會話。我有一個登錄界面的移動應用程序,當用戶使用下面的代碼輸入自己的用戶名和密碼,我把它發送到服務器(PHP頁面):如何使用android 2.2在請求頭中設置sessionID

httpMethod = new HttpPost(Constants.IOGIN_URL); 
     httpMethod.setHeader("Accept", "text/html"); 
     List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2); 
     nameValuePairs.add(new BasicNameValuePair("username", userID)); 
     nameValuePairs.add(new BasicNameValuePair("password", pass)); 
     httpMethod.setEntity(new UrlEncodedFormEntity(nameValuePairs)); 
     httpClient = new DefaultHttpClient(); 
     HttpResponse response = httpClient.execute(httpMethod); 
     HttpEntity input = response.getEntity(); 

當服務器接收這些數據用於創建一個會話該用戶併發回成功代碼。我想知道如何讀取響應頭的會話ID以及如何將它設置爲每個下一個請求頭。

+0

它會只是一個cookie,沿東西'的= SESSIONNAME的會話ID線',不管你的服務器端環境是什麼特定的名稱/值,例如PHPSESSID = blahblahblah – 2012-01-30 20:41:45

+0

當我遍歷所有的響應頭我得到三個頭與同名「Set-Cookie」的值如此 01-30 23:14:31.738:E /值(5685):CAKEPHP = b8b105b7fc845a340ceaff04f6ae4ef5; expires =星期二,2012年01月31日01:14:29 GMT;路徑=/ 我該如何利用該 – user1040987 2012-01-30 21:33:46

回答

1

我認爲這會讓你接近。 您必須抓取cookie並將它們包含在您的HTTP請求中。

從登錄請求獲得餅乾:

HttpClient httpclient = new DefaultHttpClient(); 
    try { 
     // Create a local instance of cookie store 
     CookieStore cookieStore = new BasicCookieStore(); 

     // Create local HTTP context 
     HttpContext localContext = new BasicHttpContext(); 
     // Bind custom cookie store to the local context 
     localContext.setAttribute(ClientContext.COOKIE_STORE, cookieStore); 

     HttpGet httpget = new HttpGet("http://www.yourpage/"); 

     System.out.println("executing request " + httpget.getURI()); 

     // Pass local context as a parameter 
     HttpResponse response = httpclient.execute(httpget, localContext); 
     HttpEntity entity = response.getEntity(); 

     System.out.println("----------------------------------------"); 
     System.out.println(response.getStatusLine()); 
     if (entity != null) { 
      System.out.println("Response content length: " + entity.getContentLength()); 
     } 
     List<Cookie> cookies = cookieStore.getCookies(); 
     for (int i = 0; i < cookies.size(); i++) { 
      System.out.println("Local cookie: " + cookies.get(i)); 
     } 

     // Consume response content 
     EntityUtils.consume(entity); 

     System.out.println("----------------------------------------"); 

    } finally { 
     // When HttpClient instance is no longer needed, 
     // shut down the connection manager to ensure 
     // immediate deallocation of all system resources 
     httpclient.getConnectionManager().shutdown(); 
    } 

然後將它們傳遞作爲本地contect:

HttpPost httppost = new HttpPost("http://www.yourpage/"); 

// Pass local context as a parameter  
HttpResponse response = httpclient.execute(httppost, localContext); 
+0

抱歉,但我沒有得到你的意思。我可以在哪裏獲得sessionId值?我可以在下一個請求中再次使用它嗎? – user1040987 2012-01-30 21:27:29

+0

我添加了我的答案,這應該讓你開始。 – Lars 2012-01-30 22:02:28

+0

謝謝你現在工作。我認爲問題是與服務器上的身份驗證機制 – user1040987 2012-01-31 08:57:59