Android HTTP请求中的WebView cookies

Android HTTP请求中的WebView cookies,android,Android,是否可以在HTTP请求中使用WebView的cookies?如果是,我怎么做 谢谢CookieManager是您需要的 CookieSyncManager.createInstance(context) 创建管理器 CookieSyncManager.getInstance().startSync() 在Activity.onResume()中,调用 CookieSyncManager.getInstance().stopSync() 在Activity.onPause()中 要获得即时

是否可以在HTTP请求中使用WebView的cookies?如果是,我怎么做


谢谢

CookieManager是您需要的

CookieSyncManager.createInstance(context)
创建管理器

CookieSyncManager.getInstance().startSync()
在Activity.onResume()中,调用

 CookieSyncManager.getInstance().stopSync()
在Activity.onPause()中

要获得即时同步而不是等待计时器触发,主机可以调用

 CookieSyncManager.getInstance().sync()
请注意,即使sync()也是异步进行的,所以不要在活动关闭时执行此操作

以下是您可以如何使用它:

// use cookies to remember a logged in status   
CookieSyncManager.createInstance(this);
CookieSyncManager.getInstance().startSync();
WebView webview = new WebView(this);
webview.getSettings().setJavaScriptEnabled(true);
setContentView(webview);      
webview.loadUrl([MY URL]);

编辑: 如果您想使用HttpClient来实现这一点,那么需要创建HttpContext

// Create a local instance of cookie store
CookieStore cookieStore = new BasicCookieStore();

// Create local HTTP context
HttpContext localContext = new BasicHttpContext();
// Bind custom cookie store to the local context
localContext.setAttribute(ClientContext.COOKIE_STORE, cookieStore);

HttpGet httpget = new HttpGet("http://www.google.com/"); 

System.out.println("executing request " + httpget.getURI());

// Pass local context as a parameter
HttpResponse response = httpclient.execute(httpget, localContext);

CookieManager是您需要的

CookieSyncManager.createInstance(context)
创建管理器

CookieSyncManager.getInstance().startSync()
在Activity.onResume()中,调用

 CookieSyncManager.getInstance().stopSync()
在Activity.onPause()中

要获得即时同步而不是等待计时器触发,主机可以调用

 CookieSyncManager.getInstance().sync()
请注意,即使sync()也是异步进行的,所以不要在活动关闭时执行此操作

以下是您可以如何使用它:

// use cookies to remember a logged in status   
CookieSyncManager.createInstance(this);
CookieSyncManager.getInstance().startSync();
WebView webview = new WebView(this);
webview.getSettings().setJavaScriptEnabled(true);
setContentView(webview);      
webview.loadUrl([MY URL]);

编辑: 如果您想使用HttpClient来实现这一点,那么需要创建HttpContext

// Create a local instance of cookie store
CookieStore cookieStore = new BasicCookieStore();

// Create local HTTP context
HttpContext localContext = new BasicHttpContext();
// Bind custom cookie store to the local context
localContext.setAttribute(ClientContext.COOKIE_STORE, cookieStore);

HttpGet httpget = new HttpGet("http://www.google.com/"); 

System.out.println("executing request " + httpget.getURI());

// Pass local context as a parameter
HttpResponse response = httpclient.execute(httpget, localContext);

Mm,我可以将它们添加到DefaultHttpClient中吗?我想你不明白我的意思。我想在http客户端中使用我的webview的cookies。很抱歉造成误解,这是多种技术的结合,请在这里查看,我可以将它们添加到DefaultHttpClient中吗?我想你不明白我的意思。我想在http客户端中使用我的webview的cookies。抱歉误解了,这是多种技术的结合,请在这里查看