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

2022-09-01 02:42:00

我正在尝试登录到站点并维护该会话/ 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();
}
}

为了希望其他人更好地理解我的困惑,我正在添加我知道确实有效并维护会话的代码,但是当我尝试将代码移动到我的实际应用程序中时,它打破了。

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

}

}

我知道它是有效的,你可以看到在这个代码中我消耗Content(),但是将其添加到顶部代码中似乎没有区别,所以我把它省略了。有什么想法吗?

编辑:我仍然无法让代码在保持会话运行中工作。我正在发布当前代码中的 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]

答案 1

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

请参阅示例:可在此处找到完整代码

   // 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);

答案 2

我无法让自己的代码工作(我以后可能会再次使用它),但是我在这里找到了有用的代码 Android 项目使用 httpclient --> http.client (apache),post/get 方法,我正在使用 Charlie Collins 构建的类,这类似于 ZXing Android 示例中的 Http Code。我最终可能会转到ZXing代码。


推荐