本文介绍了如何在Android和/或Java中使用HttpClient管理Cookie?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图登录到一个网站,并维护该会话/ cookie,以便服务器将识别我的登录,但我正在努力找出一种方法从响应中提取cookie,并设置为请求维护我登录。我想知道我是否应该采取标题Set-Cookie或使用CookieStore。任何帮助是非常感激。这是我的代码,我有,我认为getHeader / getCookie方法会去。评论。

I am trying to login to a site and maintain that session/cookie so that the server will recognize my login, but I am struggling to figure out a way of extracting the cookie from the response and setting into a request to maintain my login. I'm wondering if I should go about taking the header "Set-Cookie" or using a CookieStore. Any help is greatly appreciated. Here is my code that I have, with comments where I think the getHeader/getCookie methods would go.

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();
}
}

希望别人更好地理解我的混淆,我知道DOES的工作和维护一个会话,但是当我试图将代码移动到我的实际应用程序它在某处下了线。

In hopes of others better understanding my confusion am adding code that I know DOES work and maintains a session but when I tried to move the code into my actual application it broke somewhere down the line.

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();
    }    

}

}

我知道一个事实,它的工作原理,你可以看到,在这段代码我consumeContent(),但添加到顶部的代码似乎没有什么不同,所以我把它。任何想法?

I know for a fact it works, and you can see that in this code I consumeContent() but adding that to the top code didn't seem to make a difference so I left it out. Any Ideas?

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

I am still unable to get the code working in keeping a session going. I am posting the cookies from my current code, along with the cookies of the second code that is working. Perhaps someone will notice an issue, I certainly do not.

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]


推荐答案

我无法得到我自己的代码工作但我在这里找到了有用的代码,我使用Charlie Collins构建的类,类似于ZXing Android示例中的Http代码。我最终可以移动到ZXing代码。

I was unable to get my own code working (I might work on it again later), but I found useful code here Android project using httpclient --> http.client (apache), post/get method and I am using the class built by Charlie Collins, which is similar to the Http Code in the ZXing Android example. I may eventually move to the ZXing code.

这篇关于如何在Android和/或Java中使用HttpClient管理Cookie?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-22 20:05