2012-01-30 75 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