如何为Http Get请求设置cookie?

时间:2012-02-23 13:23:45

标签: android http get

无法找到那个 - 如何在HTTP GET请求中添加cookie?

List<Cookie> cookies = httpcPostlient.getCookieStore().getCookies();

Log.e("cookie_0", cookies.get(0).toString());

try {

   HttpClient client = new DefaultHttpClient();
   HttpGet request = new HttpGet();

   // request <- how do I set cookies.get(0) into here?

   request.setURI(new URI(myUrl));

   HttpResponse httpGetResponse = client.execute(request);
}

1 个答案:

答案 0 :(得分:-1)

看看这个:How do I make an http request using cookies on Android?

相关部分是这个(它只是一个复制粘贴,而不是我的代码):

import java.util.ArrayList;
import java.util.List;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.cookie.Cookie;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.protocol.HTTP;

/**
 * A example that demonstrates how HttpClient APIs can be used to perform
 * form-based logon.
 */
public class ClientFormLogin {

public static void main(String[] args) throws Exception {

    DefaultHttpClient httpclient = new DefaultHttpClient();

    HttpGet httpget = new HttpGet("https://portal.sun.com/portal/dt");

    HttpResponse response = httpclient.execute(httpget);
    HttpEntity entity = response.getEntity();

    System.out.println("Login form get: " + response.getStatusLine());
    if (entity != null) {
        entity.consumeContent();
    }
    System.out.println("Initial set of cookies:");
    List<Cookie> cookies = httpclient.getCookieStore().getCookies();
    if (cookies.isEmpty()) {
        System.out.println("None");
    } else {
        for (int i = 0; i < cookies.size(); i++) {
            System.out.println("- " + cookies.get(i).toString());
        }
    }

    HttpPost httpost = new HttpPost("https://portal.sun.com/amserver/UI/Login?" +
            "org=self_registered_users&" +
            "goto=/portal/dt&" +
            "gotoOnFail=/portal/dt?error=true");

    List <NameValuePair> nvps = new ArrayList <NameValuePair>();
    nvps.add(new BasicNameValuePair("IDToken1", "username"));
    nvps.add(new BasicNameValuePair("IDToken2", "password"));

    httpost.setEntity(new UrlEncodedFormEntity(nvps, HTTP.UTF_8));

    response = httpclient.execute(httpost);
    entity = response.getEntity();

    System.out.println("Login form get: " + response.getStatusLine());
    if (entity != null) {
        entity.consumeContent();
    }

    System.out.println("Post logon cookies:");
    cookies = httpclient.getCookieStore().getCookies();
    if (cookies.isEmpty()) {
        System.out.println("None");
    } else {
        for (int i = 0; i < cookies.size(); i++) {
            System.out.println("- " + cookies.get(i).toString());
        }
    }

    // When HttpClient instance is no longer needed, 
    // shut down the connection manager to ensure
    // immediate deallocation of all system resources
    httpclient.getConnectionManager().shutdown();        
}
}