gpt4 book ai didi

带有 https 连接和基本身份验证的 android webview。如何让这个工作?

转载 作者:IT老高 更新时间:2023-10-28 23:33:09 24 4
gpt4 key购买 nike

我一直在研究、研究和研究这个,直到我变得灰白秃顶。对于需要通过 api 级别 8+ 上的 https 连接进行 http 基本身份验证的站点,我到底如何获得 webview 工作

我有以下代码

    String email = Util.getEmail(this);
String pwd = Util.getPassword(this);
webview.getSettings().setJavaScriptEnabled(true);
webview.setHttpAuthUsernamePassword(Config.SERVER_BASE_URL, "Application", email, pwd);

// webview.setWebViewClient(new MobileWebViewClient(this));
webview.loadUrl(url);

如您所见,我确实有一个 Web View 客户端(现已注释掉),它覆盖了如下所示的 onReceivedHttpAuthRequest 方法

@Override
public void onReceivedHttpAuthRequest (WebView view, HttpAuthHandler handler, String host, String realm){
String email = Util.getEmail(wvContext);
String pwd = Util.getPassword(wvContext);
if(!pwd.equalsIgnoreCase("-1") && !pwd.equalsIgnoreCase("-1")){
handler.proceed(email, pwd);
}
}

这在没有 webview.setHttpAuthUsernamePassword 的情况下使用并且工作正常,除了这意味着向网站发出 2 个请求 - 第一个获得 401,然后客户端使用授权内容这对于少量网站来说很好流量,但流量减半(目前平均每分钟 49 个请求)是现在的游戏名称!

我读到我可以通过使用预先提供凭据

webview.setHttpAuthUsernamePassword(Config.SERVER_BASE_URL, "Application", email, pwd);

但这只会导致 Http Basic: Access denied 错误服务器基本 url 常量是站点的域名,即 https://example.com (没有页面)实际的 url 是 https://example.com/some_pages .无论我使用完整的 url 还是域,都没有区别。我已经检查了领域并且我有正确的并且我只使用了只提供电子邮件和密码的空字符串。这可能与该网站使用 https 的事实有关吗?我的代码在没有 https 的情况下在我的开发盒上似乎可以正常工作,但这可能是一个红鲱鱼。!

似乎满足我的要求的唯一堆栈溢出问题没有得到接受的答案,而且我看不到文档的帮助。

我现在因为撞到砖墙上而在我的头上留下了这么大的凹痕,我正在考虑买一顶方帽。

如果有人能给我解决这个问题的方法,我将永远欠你的债!我什至可以通过电子邮件向您发送 KitKat

最佳答案

这个简单的例子滥用了 HttpWatch 上的一个页面因为使用公开的示例会更有趣。

有问题的资源,<a href="https://www.httpwatch.com/httpgallery/authentication/authenticatedimage/default.aspx?randomgarbage" rel="noreferrer noopener nofollow">https://www.httpwatch.com/httpgallery/authentication/authenticatedimage/default.aspx?randomgarbage</a>使用基于 HTTPS 的基本身份验证,并且可以在没有像这样的身份验证失败的情况下加载(使用 Android 2.3.7 测试):

    WebView v = ...; // Your webview goes here. 
try {
HashMap<String, String> map = new HashMap<String, String>();
// This test service takes the username "httpwatch" and a random
// password. Repeating a password can lead to failure, so we create
// a decently random one using UUID.
String usernameRandomPassword = "httpwatch:" + UUID.randomUUID().toString();
String authorization = "Basic " + Base64.encodeToString(usernameRandomPassword.getBytes("UTF-8"), Base64.NO_WRAP);
map.put("Authorization", authorization);
v.loadUrl("https://www.httpwatch.com/httpgallery/authentication/authenticatedimage/default.aspx?" + System.currentTimeMillis(), map);
} catch (UnsupportedEncodingException e) {}

这适用于 ICS 和 Gingerbread。不能访问任何比这更早的东西,但是 loadUrl(String, Map<String,String>)是在 API 级别 8 中引入的,所以我不明白为什么它不应该为此工作。

澄清尿布:

为支持后续请求的身份验证,您提供 WebViewClient并执行以下操作:

    webView.setWebViewClient(new WebViewClient(){
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url, <your map containing the Authorization header>);
return true;
}
});

关于带有 https 连接和基本身份验证的 android webview。如何让这个工作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8935537/

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