2012-05-01 4 views
1

안드로이드에서 서버 클라이언트 응용 프로그램을 개발 중이며 서버 측 응용 프로그램에서 세션을 사용하고 있지만 때로는 서버에서 세션을 잃어 버렸습니다. Ps : 서버에서 https 연결을 사용합니다. 내가 세션을 개최 이것들을 사용하고안드로이드 서버 클라이언트 응용 프로그램 세션 문제

는 :

  • 내가 단일 인스턴스 DefaultHttpClient를 사용하여 모든 HTTP 요청을 사용하고 있습니다.
  • 는 난 단지 httpPost 방법을 사용하여 내가 사용
  • 에만 HTTPS 인증서 :

    schemeRegistry.register (새 제도 ("HTTPS"의 SSLSocketFactory, 443)); ClientConnectionManager cm = 새 ThreadSafeClientConnManager (params, schemeRegistry);

  • 나는 모든 HTTP 요청 후 내 쿠키를 저장합니다

    개인 무효 createSessionCookie() 나는이 일을하고 있지만 {

    List<Cookie> cookies = httpclient.getCookieStore().getCookies(); 
    
    if (! cookies.isEmpty()){ 
    
        CookieSyncManager.createInstance(ctx); 
        CookieManager cookieManager = CookieManager.getInstance(); 
    
        //sync all the cookies in the httpclient with the webview by generating cookie string 
        for (Cookie cookie : cookies){ 
    
         Cookie sessionInfo = cookie; 
    
         String cookieString = sessionInfo.getName() + "=" + sessionInfo.getValue() + "; domain=" + sessionInfo.getDomain(); 
         cookieManager.setCookie(UrlConstants.SERVICE_PRE_URL, cookieString); 
         CookieSyncManager.getInstance().sync(); 
        } 
    } 
    

    }

을, 내가 세션을 잃게됩니다. 이 문제를 해결하는 데 도움주세요, 어떤 조언을 주셔서 감사합니다 최고 감사합니다.

답변

3

쿠키를 수동으로 사용해서는 안되며 정적 인 CookieStore을 어딘가에 작성하고 HttpContext에 할당하고 요청에 해당 컨텍스트를 사용하십시오. 쿠키는 자동으로 저장되고 복원됩니다.

는 반원입니다

private static CookieStore cookieStore = new BasicCookieStore(); 
private HttpClient httpclient = new DefaultHttpClient(); 
private HttpPost post = new HttpPost("your url here"); 

그리고이 부분은 요청을 수행하는 멤버 함수로 전환 :

HttpContext ctx = new BasicHttpContext(); 
ctx.setAttribute(ClientContext.COOKIE_STORE, cookieStore); 

HttpResponse result = httpclient.execute(post,ctx); 
+1

내가 단일 인스턴스 HttpPost 방법을 사용해야합니까를? – cagryInside

+0

아니요. 오직 하나의'CookieStore' 인스턴스 만 있으면됩니다. 그러나'HttpContext'를 전달하는 한 다른 get/post 요청을 원하는만큼 사용할 수 있습니다. 같은'CookieStore'에 첨부됩니다 . – lenik

+0

감사합니다. 코드를 구현했습니다. 작동하길 바랍니다. – cagryInside

관련 문제