2012-02-23 79 views
5

無法真正弄清楚這一點 - 我如何將cookie添加到HTTP GET請求中?如何爲Http Get Request設置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); 
} 
+6

http://stackoverflow.com/questions/ 678630 /我怎麼做 - 使用Cookie上的重複請求 - 使用Cookie。 – L7ColWinters 2012-02-23 13:28:44

+0

在標頭中使用cookie:'request.setHeader(「Cookie」,「您的cookies」);' – prakash 2015-12-18 08:11:00

回答

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

這對Cookies沒有任何幫助。 – MatBee 2014-08-28 23:06:13

+0

你爲什麼不投票結束重複? – njzk2 2015-06-15 16:24:41