qui*_*int 30 cookies android httpurlconnection single-sign-on android-webview
我已经看到了关于它应该如何与旧版本一起工作的答案,
DefaultHttpClient但是没有一个很好的例子HttpURLConnection
我正在使用HttpURLConnectionWeb应用程序发出请求.在我的Android应用程序开始时,我使用CookieHandler.setDefault(new CookieManager())自动处理会话cookie,这工作正常.
在登录后的某个时刻,我想用Web应用程序向用户显示实时页面WebView而不是在后台下载数据HttpURLConnection.但是,我想使用我之前建立的相同会话来防止用户再次登录.
如何复制饼干从java.net.CookieManager使用HttpURLConnection到android.webkit.CookieManager使用的WebView,所以我可以共享会话?
tal*_*kol 51
我想建议一个完全不同的方法来解决你的问题.我们不是将cookie从一个地方复制到另一个地方(手动同步),而是让HttpURLConnection和WebViews使用相同的 cookie存储.
这完全消除了同步的需要.任何一个更新的cookie都会立即自动反映在另一个中.
为此,创建自己的java.net.CookieManager实现,它将所有请求转发到WebViews的webkit android.webkit.CookieManager.
执行:
import java.io.IOException;
import java.net.CookieManager;
import java.net.CookiePolicy;
import java.net.CookieStore;
import java.net.URI;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
public class WebkitCookieManagerProxy extends CookieManager
{
private android.webkit.CookieManager webkitCookieManager;
public WebkitCookieManagerProxy()
{
this(null, null);
}
WebkitCookieManagerProxy(CookieStore store, CookiePolicy cookiePolicy)
{
super(null, cookiePolicy);
this.webkitCookieManager = android.webkit.CookieManager.getInstance();
}
@Override
public void put(URI uri, Map<String, List<String>> responseHeaders) throws IOException
{
// make sure our args are valid
if ((uri == null) || (responseHeaders == null)) return;
// save our url once
String url = uri.toString();
// go over the headers
for (String headerKey : responseHeaders.keySet())
{
// ignore headers which aren't cookie related
if ((headerKey == null) || !(headerKey.equalsIgnoreCase("Set-Cookie2") || headerKey.equalsIgnoreCase("Set-Cookie"))) continue;
// process each of the headers
for (String headerValue : responseHeaders.get(headerKey))
{
this.webkitCookieManager.setCookie(url, headerValue);
}
}
}
@Override
public Map<String, List<String>> get(URI uri, Map<String, List<String>> requestHeaders) throws IOException
{
// make sure our args are valid
if ((uri == null) || (requestHeaders == null)) throw new IllegalArgumentException("Argument is null");
// save our url once
String url = uri.toString();
// prepare our response
Map<String, List<String>> res = new java.util.HashMap<String, List<String>>();
// get the cookie
String cookie = this.webkitCookieManager.getCookie(url);
// return it
if (cookie != null) res.put("Cookie", Arrays.asList(cookie));
return res;
}
@Override
public CookieStore getCookieStore()
{
// we don't want anyone to work with this cookie store directly
throw new UnsupportedOperationException();
}
}
Run Code Online (Sandbox Code Playgroud)
最后通过在应用程序初始化时执行此操作来使用它:
android.webkit.CookieSyncManager.createInstance(appContext);
// unrelated, just make sure cookies are generally allowed
android.webkit.CookieManager.getInstance().setAcceptCookie(true);
// magic starts here
WebkitCookieManagerProxy coreCookieManager = new WebkitCookieManagerProxy(null, java.net.CookiePolicy.ACCEPT_ALL);
java.net.CookieHandler.setDefault(coreCookieManager);
Run Code Online (Sandbox Code Playgroud)
qui*_*int 18
与之相比DefaultHttpClient,还有一些额外的步骤.关键区别在于如何访问现有的cookie HTTPURLConnection:
CookieHandler.getDefault()并将结果转换为java.net.CookieManager.getCookieStore()访问cookie存储.get()以访问给定的cookie列表URI.这是一个完整的例子:
@Override
protected void onCreate(Bundle savedInstanceState) {
// Get cookie manager for WebView
// This must occur before setContentView() instantiates your WebView
android.webkit.CookieSyncManager webCookieSync =
CookieSyncManager.createInstance(this);
android.webkit.CookieManager webCookieManager =
CookieManager.getInstance();
webCookieManager.setAcceptCookie(true);
// Get cookie manager for HttpURLConnection
java.net.CookieStore rawCookieStore = ((java.net.CookieManager)
CookieHandler.getDefault()).getCookieStore();
// Construct URI
java.net.URI baseUri = null;
try {
baseUri = new URI("http://www.example.com");
} catch (URISyntaxException e) {
// Handle invalid URI
...
}
// Copy cookies from HttpURLConnection to WebView
List<HttpCookie> cookies = rawCookieStore.get(baseUri);
String url = baseUri.toString();
for (HttpCookie cookie : cookies) {
String setCookie = new StringBuilder(cookie.toString())
.append("; domain=").append(cookie.getDomain())
.append("; path=").append(cookie.getPath())
.toString();
webCookieManager.setCookie(url, setCookie);
}
// Continue with onCreate
...
}
Run Code Online (Sandbox Code Playgroud)
Cha*_*ani 11
我用onCreate中的这一行神奇地解决了我所有的cookie问题:
CookieHandler.setDefault(new CookieManager());
| 归档时间: |
|
| 查看次数: |
43092 次 |
| 最近记录: |