如何在Android和/或Java中使用HttpClient管理cookie?我試圖登錄到一個站點并維護該會話/ cookie,以便服務器能夠識別我的登錄名,但是我正在努力尋找一種從響應中提取cookie并將其設置為保持我的登錄名的請求的方法。我想知道是否應該使用標題“ Set-Cookie”或使用CookieStore。任何幫助是極大的贊賞。這是我的代碼,并附有注釋,我認為getHeader / getCookie方法可以使用。public class Http{DefaultHttpClient client = new DefaultHttpClient();HttpGet request;HttpEntity entity;HttpResponse response;HttpPost post;CookieStore cookieStore = new BasicCookieStore();HttpContext localContext = new BasicHttpContext();public static void setContext(){ localContext.setAttribute(ClientContext.COOKIE_STORE, cookieStore);}public static void getPage(String url) throws Exception{ request = new HttpGet(url); response = client.execute(request, localContext); PARSER.preParse(url, response);}public static HttpResponse postPage(List<NameValuePair> params, String host, String action) throws Exception{ post = new HttpPost(host + action); post.setEntity(new UrlEncodedFormEntity(params, HTTP.UTF_8)); response = client.execute(post, localContext); entity = response.getEntity(); if(entity != null) { entity.consumeContent(); } return response;}public void destoyHttp(){ client.getConnectionManager().shutdown();}}為了希望其他人能更好地理解我的困惑,我添加了我知道確實可以正常工作并維護會話的代碼,但是當我嘗試將代碼移至我的實際應用程序中時,它陷入了混亂。
添加回答
舉報
0/150
提交
取消
