android - 如何为 Http Get 请求设置 cookie?

标签 android http get

<分区>

真的想不通 - 如何将 cookie 添加到 HTTP GET 请求?

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

最佳答案

看看这个: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();        
}
}

关于android - 如何为 Http Get 请求设置 cookie?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9413939/

相关文章:

image - 两张照片(大)具有相同字节数的几率是多少?

php - 使用 Laravel 5 格式化 URL 查询字符串(如 Stack Overflow)

json - 访问 JSON 对象 Prop - Angular JS

android - 使用sendMultimediaMessage发送和读取MMS

android - 数据报套接字请求目的地址

java - ListView SetAdapter 错误

http - 为 Web API 驱动程序使用 HTTP 持久连接?

linux - 通过端口 8080 上的 SSH 的 HTTP XML 数据

Java 休息 : @GET and @PUT at the same path?

java - 通知数据集更改后回收器 View 未更新