Android Emulator中的HTTP代理适用于浏览器,而不适用于WebView

Android Emulator中的HTTP代理适用于浏览器,而不适用于WebView,android,Android,我的开发环境落后于HTTP代理。在Android emulator中,我可以在wifi首选项中设置我的代理地址和凭据。然后,当我在浏览器中查看页面时,系统会提示我重新输入指定主机的凭据。不知道为什么我必须再次输入它们,但它可以工作,我可以查看页面 然后,我有一个应用程序,它带有一个Web视图,可以加载相同的页面。该应用程序具有internet权限: <uses-permission android:name="android.permission.INTERNET"/> <us

我的开发环境落后于HTTP代理。在Android emulator中,我可以在wifi首选项中设置我的代理地址和凭据。然后,当我在浏览器中查看页面时,系统会提示我重新输入指定主机的凭据。不知道为什么我必须再次输入它们,但它可以工作,我可以查看页面

然后,我有一个应用程序,它带有一个Web视图,可以加载相同的页面。该应用程序具有internet权限:

<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
但是。。。当我尝试查看页面时,我得到代理的错误页面,说用户名和密码输入不正确。这对我来说意味着应用程序正在成功读取代理地址,但就像浏览器没有读取凭据一样。然后,它就无法在完成请求之前显示与浏览器请求凭据相同的对话框


是否有办法启用此对话框(是否内置?)或手动指定代理详细信息?

查看android浏览器源代码,以下内容很清楚:

  • WebView可以访问设置中配置的HTTP代理主机名和端口
  • 网络视图(和应用程序)无权访问“设置”中配置的HTTP代理用户名和密码
  • WebView像对待其他HTTP身份验证请求一样对待代理身份验证请求,通过将
    主机设置为
    “{proxy}:{port}”
    域设置为空字符串,触发附加的
    WebViewClient
    onReceivedHttpAuthRequest
  • 浏览器正在使用其自己的自定义对话框来处理身份验证请求,此对话框不向其他应用公开
  • 因此,从Android浏览器复制代码和对话框布局的最简单方法是:

  • 复制到您自己的项目中
  • 抄袭
  • 从中复制相关字符串
  • 创建一个实现
    WebViewClient
    的新类,并从中复制
    OnReceiveDhtPauthRequest
    方法
  • 将该方法修改为不依赖于
    mPagesDialogHandler
    <代码>上下文是您的活动

    HttpAuthenticationDialog dialog = new HttpAuthenticationDialog(context, host, realm);
    
    dialog.setOkListener(new HttpAuthenticationDialog.OkListener() {
        public void onOk(String host, String realm, String username, String password) {
            handler.proceed(username, password);
        }
    });
    
    dialog.setCancelListener(new HttpAuthenticationDialog.CancelListener() {
        public void onCancel() {
            handler.cancel();
        }
    });
    
    dialog.show();
    
  • 将此新的
    WebViewClient
    用于web视图


  • 根据您的回答,我得到的对话框,输入用户名和密码后,我无法登录。我需要再次加载webview url吗?
    HttpAuthenticationDialog dialog = new HttpAuthenticationDialog(context, host, realm);
    
    dialog.setOkListener(new HttpAuthenticationDialog.OkListener() {
        public void onOk(String host, String realm, String username, String password) {
            handler.proceed(username, password);
        }
    });
    
    dialog.setCancelListener(new HttpAuthenticationDialog.CancelListener() {
        public void onCancel() {
            handler.cancel();
        }
    });
    
    dialog.show();