如何在Android和/或Java中使用HttpClientpipe理Cookie?

我试图login到一个网站,并维护该会话/ cookie,以便服务器将识别我的login,但我正在努力找出一种方法,从响应中提取cookie并设置为维护我的login请求。 我想知道是否应该采取头“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(); } } 

为了让别人更好地理解我的困惑,我添加了一些代码,我知道是否能够工作并维护一个会话,但是当我试图将代码移到我的实际应用程序中时,这个代码就被打破了。

 public class HttpClientTest extends Activity{ DefaultHttpClient client = new DefaultHttpClient(); HttpGet request; HttpEntity entity; List<Cookie> cookies; HttpResponse response; HttpPost post; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); try { getRequest(); } catch (Exception e) { Log.d("My Activity", "Failed"); e.printStackTrace(); } } public void getRequest() throws Exception { final String TAG = "MyActivity"; request = new HttpGet("http://gc.gamestotal.com/i.cfm?p=login&se=4"); response = client.execute(request); String action = "i.cfm?&1028&p=login&se=4"; String yourServer = "http://gc.gamestotal.com/"; post = new HttpPost(yourServer + action); List<NameValuePair> params = new ArrayList<NameValuePair>(); params.add(new BasicNameValuePair("nic", "myusername")); params.add(new BasicNameValuePair("password", "mypassword")); params.add(new BasicNameValuePair("server", "4")); post.setEntity(new UrlEncodedFormEntity(params, HTTP.UTF_8)); response = client.execute(post); entity = response.getEntity(); if(entity != null){ entity.consumeContent(); } request = new HttpGet("http://gc.gamestotal.com/i.cfm?f=com_empire&cm=3"); response = client.execute(request); if(entity != null) { entity.consumeContent(); } } } 

我知道一个事实,它可以工作,你可以看到在这个代码中我使用了ContentContent(),但是把它添加到最上面的代码看起来没有什么区别,所以我把它排除了。 有任何想法吗?

编辑:我仍然无法让代码工作保持会议进行。 我从我当前的代码,以及正在工作的第二个代码的cookie张贴Cookie。 也许有人会注意到一个问题,我当然不会。

 08-31 06:53:50.318: VERBOSE/SFGC(496): - [version: 0][name: CFID][value: 26651316][domain: gc.gamestotal.com][path: /][expiry: Sun Sep 27 00:00:00 GMT+00:00 2037] 08-31 06:53:50.329: VERBOSE/SFGC(496): - [version: 0][name: CFTOKEN][value: 96917381][domain: gc.gamestotal.com][path: /][expiry: Sun Sep 27 00:00:00 GMT+00:00 2037] 08-31 06:53:50.329: VERBOSE/SFGC(496): - [version: 0][name: CFCLIENT_SFGC_106Y][value: CFTOKEN2%3D735125416524430%23COUNTRY%3D56%23DOWNLOADFLAG%3D0%23OCHAT%3D1%23PM%5FDATE%3D%7Bts+%272010%2D08%2D31+14%3A53%3A45%27%7D%23REGION%3D3%23SCREEN%3D800%23S%5FGC%5FIMAGELOC%3Di%2Fw%2F%23TIMEZONE%3D%2D600%23][domain: gc.gamestotal.com][path: /][expiry: Sun Sep 27 00:00:00 GMT+00:00 2037] 08-31 06:53:50.339: VERBOSE/SFGC(496): - [version: 0][name: CFGLOBALS][value: HITCOUNT%3D2%23LASTVISIT%3D%7Bts+%272010%2D08%2D31+14%3A53%3A45%27%7D%23TIMECREATED%3D%7Bts+%272010%2D08%2D31+14%3A53%3A39%27%7D%23][domain: gc.gamestotal.com][path: /][expiry: Sun Sep 27 00:00:00 GMT+00:00 2037] 08-31 06:53:51.938: VERBOSE/SFGC(496): - [version: 0][name: CFID][value: 26651316][domain: gc.gamestotal.com][path: /][expiry: Sun Sep 27 00:00:00 GMT+00:00 2037] 08-31 06:53:51.938: VERBOSE/SFGC(496): - [version: 0][name: CFTOKEN][value: 96917381][domain: gc.gamestotal.com][path: /][expiry: Sun Sep 27 00:00:00 GMT+00:00 2037] 08-31 06:53:51.938: VERBOSE/SFGC(496): - [version: 0][name: CFCLIENT_SFGC_106Y][value: CFTOKEN2%3D735125416524430%23COUNTRY%3D56%23DOWNLOADFLAG%3D0%23OCHAT%3D1%23PM%5FDATE%3D%7Bts+%272010%2D08%2D31+14%3A53%3A45%27%7D%23REGION%3D3%23SCREEN%3D800%23S%5FGC%5FIMAGELOC%3Di%2Fw%2F%23TIMEZONE%3D%2D600%23][domain: gc.gamestotal.com][path: /][expiry: Sun Sep 27 00:00:00 GMT+00:00 2037] 08-31 06:53:51.948: VERBOSE/SFGC(496): - [version: 0][name: CFGLOBALS][value: HITCOUNT%3D3%23LASTVISIT%3D%7Bts+%272010%2D08%2D31+14%3A53%3A46%27%7D%23TIMECREATED%3D%7Bts+%272010%2D08%2D31+14%3A53%3A39%27%7D%23][domain: gc.gamestotal.com][path: /][expiry: Sun Sep 27 00:00:00 GMT+00:00 2037] 08-31 06:52:41.628: DEBUG/MyActivity(469): - [version: 0][name: CFID][value: 26651274][domain: gc.gamestotal.com][path: /][expiry: Sun Sep 27 00:00:00 GMT+00:00 2037] 08-31 06:52:41.638: DEBUG/MyActivity(469): - [version: 0][name: CFTOKEN][value: 58361320][domain: gc.gamestotal.com][path: /][expiry: Sun Sep 27 00:00:00 GMT+00:00 2037] 08-31 06:52:41.638: DEBUG/MyActivity(469): - [version: 0][name: CFCLIENT_SFGC_106Y][value: CFTOKEN2%3D735125815099420%23COUNTRY%3D56%23DOWNLOADFLAG%3D0%23OCHAT%3D1%23PM%5FDATE%3D%7Bts+%272010%2D08%2D31+14%3A52%3A36%27%7D%23REGION%3D3%23SCREEN%3D800%23S%5FGC%5FIMAGELOC%3Di%2Fw%2F%23TIMEZONE%3D%2D600%23][domain: gc.gamestotal.com][path: /][expiry: Sun Sep 27 00:00:00 GMT+00:00 2037] 08-31 06:52:41.648: DEBUG/MyActivity(469): - [version: 0][name: CFGLOBALS][value: HITCOUNT%3D2%23LASTVISIT%3D%7Bts+%272010%2D08%2D31+14%3A52%3A36%27%7D%23TIMECREATED%3D%7Bts+%272010%2D08%2D31+14%3A52%3A33%27%7D%23][domain: gc.gamestotal.com][path: /][expiry: Sun Sep 27 00:00:00 GMT+00:00 2037] 08-31 06:52:44.138: DEBUG/MyActivity(469): - [version: 0][name: CFID][value: 26651274][domain: gc.gamestotal.com][path: /][expiry: Sun Sep 27 00:00:00 GMT+00:00 2037] 08-31 06:52:44.138: DEBUG/MyActivity(469): - [version: 0][name: CFTOKEN][value: 58361320][domain: gc.gamestotal.com][path: /][expiry: Sun Sep 27 00:00:00 GMT+00:00 2037] 08-31 06:52:44.138: DEBUG/MyActivity(469): - [version: 0][name: CFCLIENT_SFGC_106Y][value: CFTOKEN2%3D735125815099420%23COUNTRY%3D56%23DOWNLOADFLAG%3D0%23OCHAT%3D1%23PM%5FDATE%3D%7Bts+%272010%2D08%2D31+14%3A52%3A36%27%7D%23REGION%3D3%23SCREEN%3D800%23S%5FGC%5FIMAGELOC%3Di%2Fw%2F%23TIMEZONE%3D%2D600%23][domain: gc.gamestotal.com][path: /][expiry: Sun Sep 27 00:00:00 GMT+00:00 2037] 08-31 06:52:44.138: DEBUG/MyActivity(469): - [version: 0][name: CFGLOBALS][value: HITCOUNT%3D4%23LASTVISIT%3D%7Bts+%272010%2D08%2D31+14%3A52%3A38%27%7D%23TIMECREATED%3D%7Bts+%272010%2D08%2D31+14%3A52%3A33%27%7D%23][domain: gc.gamestotal.com][path: /][expiry: Sun Sep 27 00:00:00 GMT+00:00 2037] 

你需要使用HttpContext。 将Cookie存储设置为上下文,并在执行方法中使用HttpGet / HttpPost传递上下文。 希望这应该有所帮助。

看例子:完整的代码可以在这里find

  // 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.google.com/"); System.out.println("executing request " + httpget.getURI()); // Pass local context as a parameter HttpResponse response = httpclient.execute(httpget, localContext); 

我无法得到我自己的代码工作(我可能会在以后再工作),但我发现有用的代码在这里Android项目使用httpclient – > http.client(apache),post / get方法和我正在使用的类由Charlie Collins编写,与ZXing Android示例中的Http代码类似。 我最终可能会转向ZXing代码。

对我来说,它没有工作,直到我使HttpContext和CookieStore静态,留下所有请求。