android中的GET和POST请求如何添加HttpCookies?
How to add HttpCookies to GET and POST requests in android?
我正在使用 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请求中附加cookies,以维持会话。我尝试了以下代码,但它不起作用。
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);
一种方法,尽管这里的 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();
我会建议您使用 HttpUrlConnection
class。是Google.
推荐的
HTTP cookie 是使用 HTTP headers 发送的。只需将 cookie 本身作为值提供给 "Cookie" header。就这样。发送带有请求的 cookie 只是向其添加一个新的 HTTP header:
URL url = new URL("http://www.alabala.com");
URLConnection connection = url.openConnection();
// set the cookie
connection.setRequestProperty("Cookie", "my-cookie-value");
...
conn.getOutputStream();
在Apache
的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();
好吧,我自己想出了答案。互联网上发布了不同的解决方案,但其中 none 提到的很清楚,因为我对后端开发一无所知。
在以下代码段中,假设您在上次响应中检索到一个 cookie(类似于 "w8rhn29wr208n4rc2mr29cmn2rn2m12",一些随机值)。现在如果你想把它附加到下一个请求,你必须在请求中将它添加为 HEADER。 URL 请求中的 headers 的格式为 "some_key=some_value"。 "some_key" 实际上是在服务器端编程中定义的。对于同一请求中的多个 cookie,您必须在同一请求中附加多个 headers。
其余过程正常,关于附加 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);
我正在使用 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请求中附加cookies,以维持会话。我尝试了以下代码,但它不起作用。
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);
一种方法,尽管这里的 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();
我会建议您使用 HttpUrlConnection
class。是Google.
HTTP cookie 是使用 HTTP headers 发送的。只需将 cookie 本身作为值提供给 "Cookie" header。就这样。发送带有请求的 cookie 只是向其添加一个新的 HTTP header:
URL url = new URL("http://www.alabala.com");
URLConnection connection = url.openConnection();
// set the cookie
connection.setRequestProperty("Cookie", "my-cookie-value");
...
conn.getOutputStream();
在Apache
的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();
好吧,我自己想出了答案。互联网上发布了不同的解决方案,但其中 none 提到的很清楚,因为我对后端开发一无所知。
在以下代码段中,假设您在上次响应中检索到一个 cookie(类似于 "w8rhn29wr208n4rc2mr29cmn2rn2m12",一些随机值)。现在如果你想把它附加到下一个请求,你必须在请求中将它添加为 HEADER。 URL 请求中的 headers 的格式为 "some_key=some_value"。 "some_key" 实际上是在服务器端编程中定义的。对于同一请求中的多个 cookie,您必须在同一请求中附加多个 headers。
其余过程正常,关于附加 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);