如何在Android中添加HttpCookies到GET和POST请求?

时间:2015-04-09 06:59:29

标签: android httpcookie

我正在使用HttpCookies维护服务器上的在线会话。在登录时,通过POST请求,我使用以下代码获取cookie。

HttpPost  httpPost = new HttpPost(url);
CookieStore store =  ((DefaultHttpClient)client).getCookieStore();
        if(params != null)
            httpPost.setEntity(new UrlEncodedFormEntity(params));
        httpResponse = httpClient.execute(httpPost);

        cookies= httpClient.getCookieStore().getCookies();

        if(cookies.isEmpty())
        {
            Log.d("Cookies", "None!");
        }
        else
        {
            for (Cookie c : cookies) {
                Log.d("Cookies","Name ["+c.getName()+"] - Val ["+c.getValue()+"] - Domain ["+c.getDomain()+"] - Path ["+c.getPath()+"]");
                store.addCookie(c);
            }
        }

但问题是,我无法在下一个GET请求中附加cookie,以便维护会话。我尝试了以下代码,但它无法正常工作。

HttpContext ctx = new BasicHttpContext();
store.addCookie(singleCookie);
ctx.setAttribute(ClientContext.COOKIE_STORE, store);
Log.d("Servicehandler", singleCookie.getValue());
HttpGet request = new HttpGet();
request.setURI(new URI(url));
HttpResponse r_response = client.execute(request, ctx);

4 个答案:

答案 0 :(得分:0)

一种方法,虽然这里的cookie是手动保存的(我不建议)。

AbstractHttpClient client = new DefaultHttpClient();
        //setup url
        Uri.Builder uriBuilder = Uri.parse(mRestUrl).buildUpon();
        Uri uri = uriBuilder.build();
        // request is either HttpPost or HttpGet
        request.setURI(new URI(uri.toString()));

        // Here we add cookies to cookie store of a client 
        //(cookies saved manually)
        CookieStore cookieStore = client.getCookieStore();
        for (Cookie cookie : cookieList) {
            cookieStore.addCookie(cookie);
        }

        HttpResponse response = client.execute(request);
        //in case you want to save cookies manually
        cookieList = client.getCookieStore().getCookies();
        //response
        HttpEntity responseEntity = response.getEntity();

答案 1 :(得分:0)

我会建议你使用HttpUrlConnection课程。这是谷歌推荐的。

使用HTTP标头发送HTTP Cookie。只需提供" Cookie"标头以cookie本身作为值。这就是全部。使用请求发送cookie只是向其添加新的HTTP标头:

URL url = new URL("http://www.alabala.com");
URLConnection connection = url.openConnection();
// set the cookie
connection.setRequestProperty("Cookie", "my-cookie-value");   
...
conn.getOutputStream();

答案 2 :(得分:0)

ApacheHttpClient中,它是这样的:

HttpMethod method = new GetMethod();
//method.getParams().setCookiePolicy(CookiePolicy.RFC_2109);
method.getParams().setCookiePolicy(CookiePolicy.IGNORE_COOKIES);
method.setRequestHeader("Cookie", "special-cookie=value");

如果你使用HttpUrlConnection,也许你需要尝试:

conn.setRequestProperty("Cookie", "jsessionid=xxxxx;param1=a"); 
conn.connect();

答案 3 :(得分:0)

好吧,我自己想出了答案。在互联网上发布了不同的解决方案,但没有一个明确提到事情,因为我不知道后端开发。

在下面的代码片段中,我们假设你在最后一个响应中检索了一个cookie(类似于" w8rhn29wr208n4rc2mr29cmn2rn2m12",一些随机值)。现在,如果要将其附加到下一个请求,则必须在请求中将其添加为HEADER。 URL请求中的标题的格式为" some_key = some_value"。 " some_key"实际上是在服务器端编程中定义的。对于同一请求中的多个cookie,您必须在同一请求中附加多个标头。

其余的程序是正常的,关于附加网址,执行它并获得响应。

String url= "www.test.in/test_cookie_link";
HttpGet request = new HttpGet();
//cookie_received_in_last_response = w8rhn29wr208n4rc2mr29cmn2rn2m12
//add the cookie as header to the GET request
request.addHeader("cookie","my_key=" + cookie_received_in_last_response);
//attach the url to GET request
request.setURI(new URI(url));
//execute the request
HttpResponse r_response = client.execute(request);