如何在android中添加HttpCookie来获取和发布请求?
我正在使用HttpCookies在服务器上维护在线会话。在登录时,通过POST请求,我使用以下代码获取cookies如何在android中添加HttpCookie来获取和发布请求?,android,httpcookie,Android,Httpcookie,我正在使用HttpCookies在服务器上维护在线会话。在登录时,通过POST请求,我使用以下代码获取cookies HttpPost httpPost = new HttpPost(url); CookieStore store = ((DefaultHttpClient)client).getCookieStore(); if(params != null) httpPost.setEntity(new UrlEncodedFormEntity(p
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);
一种方法是手动保存cookies,我不建议这样做
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();
我建议您使用HttpUrlConnection类。这是谷歌推荐的
HTTP Cookie使用HTTP头发送。只需将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();
在的HttpClient中,它如下所示:
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();
我自己找到了答案。有不同的解决方案张贴在互联网上,但没有一个提到清楚的事情,因为我不知道有关后端开发 在下面的代码段中,假设您检索到了一个类似w8rhn29wr208n4rc2mr29cmn2rn2m12的cookie,它是最后一个响应中的一个随机值。现在,如果您想将其附加到下一个请求,您必须将其作为头添加到请求中。URL请求中的头的形式为some_key=some_value。some_键实际上是在服务器端编程中定义的。对于同一请求中的多个cookie,您必须在同一请求中附加多个标头 程序的其余部分是正常的,关于附加url、执行url和获取响应
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);
u.openConnection中的u是什么?抱歉,我错过了一行:检查我的更新答案。setCookies和storeCookies方法未标记为已识别/已解析。此外,我在哪里将以前收到的cookies附加到我正在生成的新GET请求?请查看我的更新答案。很抱歉我之前发布的误导性信息。对不起,但您将方法与客户端关联到了哪里?特殊cookie=value表示什么呢?第一个问题,HttpClient.executemethod,而方法可以是HttpGet-HttpPost等等。其次,Cookie的参数可以是key=value对,您可以将其发送到服务器,然后服务器可以响应客户机。