【发布时间】:2012-11-25 23:30:09
【问题描述】:
我有一个应用程序,它应该向 URL 发送 GET 请求并发送一些 cookie。我一直在查看 BasicCookieStore 和 Cookie 类的一些代码示例,但我无法弄清楚如何使用它们。谁能指出我正确的方向?
【问题讨论】:
标签: java android cookies http-get cookiestore
我有一个应用程序,它应该向 URL 发送 GET 请求并发送一些 cookie。我一直在查看 BasicCookieStore 和 Cookie 类的一些代码示例,但我无法弄清楚如何使用它们。谁能指出我正确的方向?
【问题讨论】:
标签: java android cookies http-get cookiestore
要使用 cookie,您需要以下内容:
CookieStore cookieStore = new BasicCookieStore();
DefaultHttpClient httpclient = new DefaultHttpClient();
HttpContext ctx = new BasicHttpContext();
ctx.setAttribute(ClientContext.COOKIE_STORE, cookieStore);
HttpGet get = new HttpGet("your URL here");
HttpResponse response = httpclient.execute(get,ctx);
如果您想在请求之间保留 cookie,则必须为每个请求重用 cookieStore 和 ctx。
另外,您可以阅读您的cookieStore 以了解其中的内容:
List<Cookie> cookies = cookieStore.getCookies();
if( !cookies.isEmpty() ){
for (Cookie cookie : cookies){
String cookieString = cookie.getName() + " : " + cookie.getValue();
Log.info(TAG, cookieString);
}
}
【讨论】:
addCookie() 的CookieStore 方法:developer.android.com/reference/org/apache/http/client/…