2010-08-04 19 views
2

我能夠使用此處的示例:http://www.androidsnippets.org/snippets/36/index.html併成功獲取Webesite的「HTTP/1.1 OK」響應我正在發送HttpPost以及用戶憑證。但是,我無法使用HttpGet來進一步瀏覽本網站上的其他頁面。Android:使用來自HttpPost的Cookie用於HttpGet的示例

任何人都可以請讓我知道,怎麼回事。我很抱歉 - 我對Java很陌生。

+0

看看:http://stackoverflow.com/questions/3858593/android-http-get-session-cookie – Blundell 2011-04-07 21:00:21

回答

6

我的猜測是,當網站獲得帖子並登錄用戶時,它會在響應中設置cookie以指示用戶已登錄,然後在隨後的Get中需要這些cookie。

您需要像做以下(這是從一個更大的應用程序借來的,可能不編譯開箱的)

DefaultHttpClient mHttpClient = new DefaultHttpClient(); 
BasicHttpContext mHttpContext = new BasicHttpContext(); 
CookieStore mCookieStore  = new BasicCookieStore();   
mHttpContext.setAttribute(ClientContext.COOKIE_STORE, mCookieStore); 

這將設置HTTP上下文中的cookie存儲,並然後在Get和Post上使用該上下文。例如...

HttpResponse response = mHttpClient.execute(mRequest, mHttpContext); 

在封面HTTP客戶端將存儲來自響應的cookie,並將它們添加到請求。

相關問題