gpt4 book ai didi

android - 如何使用 session ID和用户ID详细信息在Webview中保持登录到我的域URL

转载 作者:行者123 更新时间:2023-11-29 01:13:31 26 4
gpt4 key购买 nike

我正在使用本地google +登录oauth2令牌从服务器获取响应,并且正在设置Cookie:

String cookieSessionString = "JSESSIONID" + "=" + i.getStringExtra("C_session_id");
String cookieEmailString = "useremail" + "=" + i.getStringExtra("C_user_email");
String cookieNameString = "username" + "=" + i.getStringExtra("C_user_name");
String cookieUseridString = "userid" + "=" + i.getStringExtra("C_user_id");
CookieManager cookieManager = CookieManager.getInstance();
CookieSyncManager cookieSyncManager = CookieSyncManager.createInstance(webView.getContext());
cookieManager.setCookie("mydomain.co", cookieSessionString);
cookieManager.setCookie("mydomain.co", cookieEmailString);
cookieManager.setCookie("mydomain.co", cookieNameString);
cookieManager.setCookie("mydomain.co", cookieUseridString);
cookieManager.setAcceptCookie(true);
cookieSyncManager.getInstance().sync();


在网络视图中,我打电话:

       webView.getSettings().setAppCacheEnabled(true);
WebViewDatabase.getInstance(this).clearHttpAuthUsernamePassword();
webView.getSettings().setDatabaseEnabled(true);
webView.getSettings().setDomStorageEnabled(true);
webSettings.getCacheMode();
webView.loadUrl(base_url);
webView.setWebViewClient(new WebViewClient() {
@Override
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
startActivity(new Intent(MainActivity.this, NoDataActivity.class).putExtra("Activity", "Main.class"));
super.onReceivedError(view, errorCode, description, failingUrl);
}
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
@Override
public void onLoadResource( WebView view, String url ){
CookieManager cookieManager = CookieManager.getInstance();
cookie = cookieManager.getCookie(url);
Log.d("ONloadResource","cookie is"+cookie);
}
@Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
CookieManager cookieManager = CookieManager.getInstance();
cookieManager.setCookie(url, cookie);
progressBar.setVisibility(View.GONE);
progressBar.setProgress(100);
if (url.contains("/ccavResponseHandler.jsp")) {
webView.loadUrl("javascript:window.HTMLOUT.processHTML ('<head>'+document.getElementsByTagName('html')[0].innerHTML+'</head>');");
}
}
@Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
super.onPageStarted(view, url, favicon);
progressBar.setVisibility(View.VISIBLE);
progressBar.setProgress(5);
}

});


并且还尝试发送标头,例如:

 extraHeaders.put("JSESSIONID", i.getStringExtra("C_session_id"));
extraHeaders.put("username", i.getStringExtra("C_user_name"));
extraHeaders.put("useremail", i.getStringExtra("C_user_email"));
webView.loadUrl(url, extraHeaders);`


没在webview登录中工作..!

任何人都可以帮助我,如何在我的域URL的webview中使用会话ID和用户电子邮件ID登录。

最佳答案

您可能会发现自己在Android中所做的是网络身份验证。大多数网站都会发布cookie来跟踪会话ID并保持用户登录。为了维持此身份验证,您将需要在活动之间以及http客户端和webview之间保持cookie同步。

我最终做的方法似乎很有效,它是创建一个扩展Application的类,然后定义一个HttpClient以便在Application的其余部分中使用。该代码如下所示:

public class AppSettings extends Application {
private static final DefaultHttpClient client = createClient();

@Override
public void onCreate(){
}

static DefaultHttpClient getClient(){
return client;
}
private static DefaultHttpClient createClient(){
BasicHttpParams params = new BasicHttpParams();
SchemeRegistry schemeRegistry = new SchemeRegistry();
schemeRegistry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80));
final SSLSocketFactory sslSocketFactory = SSLSocketFactory.getSocketFactory();
schemeRegistry.register(new Scheme("https", sslSocketFactory, 443));
ClientConnectionManager cm = new ThreadSafeClientConnManager(params, schemeRegistry);
DefaultHttpClient httpclient = new DefaultHttpClient(cm, params);
httpclient.getCookieStore().getCookies();
return httpclient;
}


此类还创建一个适用于AsyncTasks的HttpClient,并同时发出多个http请求。通过将ThreadSafeClientConnManager用于ClientConnectionManager,我们可以在AsyncTasks中运行http请求,而无需在按下后退按钮并启动第二个或第三个按钮时强制关闭。

它还会创建一个默认的Cookie存储,可以在您的所有活动中对其进行访问。您可以通过调用getClient()方法在其他活动中使用它。

例如

public class SomeActivity extends Activity {
static DefaultHttpClient mClient = AppSettings.getClient();
...
try {

HttpGet httpget = new HttpGet('URL');
HttpResponse response;
response = mClient.execute(httpget);
...
}
...
}


如果您发现有必要使用Webview,并且需要它来访问和使用与您的客户端相同的cookie。您可以使用CookieManager来同步客户端的Cookie存储:(在onPageStarted方法中)

DefaultHttpClient mClient = AppSettings.getClient();


Cookie sessionInfo;
List<Cookie> cookies = mClient.getCookieStore().getCookies();

if (! cookies.isEmpty()){
CookieSyncManager.createInstance(getApplicationContext());
CookieManager cookieManager = CookieManager.getInstance();

for(Cookie cookie : cookies){
sessionInfo = cookie;
String cookieString = sessionInfo.getName() + "=" + sessionInfo.getValue() + "; domain=" + sessionInfo.getDomain();
cookieManager.setCookie("example.com", cookieString);
CookieSyncManager.getInstance().sync();
}
}


您将需要使用正确的域切换example.com。

关于android - 如何使用 session ID和用户ID详细信息在Webview中保持登录到我的域URL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41395173/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com