在Android模拟器HTTP代理适用于浏览器,而不是为的WebView [英] HTTP proxy in Android Emulator works for browser, not for WebView

查看:167
本文介绍了在Android模拟器HTTP代理适用于浏览器,而不是为的WebView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的开发环境是落后和HTTP代理。在Android模拟器,我可以把我的代理服务器地址和凭据在WiFi preferences。然后,当我在浏览器中查看网页,我得到提示重新输入我的凭据,我指定的主机。不知道为什么我必须再次输入,但它的作品,我可以查看网页。

My development environment is behind and HTTP proxy. In the Android emulator, I am able to set my proxy address and credentials in the wifi preferences. Then when I view the page in the browser, I get prompted to reenter my credentials for the host I specified. Not sure why I have to enter them again, but it works and I'm able to view the page.

然后,我有一个web视图,它加载相同页面的应用程序。该应用程序有互联网的权限:

I then have an app with a WebView which loads the same page. The app has internet permission:

<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>

和支持平台通知:

WebView.enablePlatformNotifications();

但是......当我尝试查看的页面,我得到了代理的错误页面,指出用户名和密码不正确。这表明,我认为应用程序被成功读取代理地址,但像浏览器未阅读的凭据。它是那么缺乏表现出同样的对话框,要求输入凭据完成请求之前,浏览器的能力。

But... when I try viewing the page I get the proxy's error page saying the username and password weren't entered correctly. This suggests to me that the app is successfully reading the proxy address, but like the browser isn't reading the credentials. It is then lacking the ability to show the same dialog as the browser asking for the credentials before completing the request.

有没有一种方法,使这个对话框(这是建于?)还是有办法来手动指定代理的详细信息?

Is there a way to enable this dialog (is it built in?) or a way to manually specify the proxy details?

推荐答案

纵观Android浏览器源$ C ​​$ C,以下是明确的:

Looking at the android browser source code, the following is clear:

  1. 在WebViews有权访问HTTP代理服务器的主机名和端口的配置设置。
  2. WebViews(和应用程序)不具有访问HTTP代理服务器的用户名和密码配置设置。
  3. 在WebViews像对待其他HTTP认证请求代理身份验证请求,用<$ C触发附加 WebViewClient onReceivedHttpAuthRequest $ C>主机设置为{代理}:{端口}领域设置为空字符串。
  4. 在浏览器是使用自己的自定义对话框来处理身份验证请求,而这种对话不会暴露给其他应用程序。
  1. WebViews have access to the HTTP proxy hostname and port as configured in Settings.
  2. WebViews (and apps) do not have access to the HTTP proxy username and password as configured in Settings.
  3. WebViews treat proxy authentication requests like other HTTP authentication requests, triggering onReceivedHttpAuthRequest of the attached WebViewClient with host set to "{proxy}:{port}" and realm set to an empty string.
  4. The browser is using its own custom dialog to handle authentication requests, and this dialog is not exposed to other apps.

因此​​,重复Android浏览器中的code和对话框布局最简单的方法:

As such, the easiest approach to duplicate the code and dialog layout from Android Browser:

  1. 复制<一href="https://github.com/android/platform_packages_apps_browser/blob/master/res/layout/http_authentication.xml">res/layout/http_authentication.xml到自己的项目。
  2. 复制<一href="https://github.com/android/platform_packages_apps_browser/blob/master/src/com/android/browser/HttpAuthenticationDialog.java">src/com/android/browser/HttpAuthenticationDialog.java
  3. 复制相关的字符串,从<一个href="https://github.com/android/platform_packages_apps_browser/blob/master/res/values/strings.xml">res/values/strings.xml
  4. 创建一个新的类,它实现 WebViewClient 和复制 onReceivedHttpAuthRequest 方法<一href="https://github.com/android/platform_packages_apps_browser/blob/master/src/com/android/browser/Controller.java#L973"><$c$c>com.android.browser.Controller.
  5. 修改为不依赖于 mPagesDialogHandler 的方法。 上下文为您的活动。

  1. Copy res/layout/http_authentication.xml into your own project.
  2. Copy src/com/android/browser/HttpAuthenticationDialog.java
  3. Copy relevant strings from res/values/strings.xml
  4. Create a new class that implements WebViewClient and copy the onReceivedHttpAuthRequest method from com.android.browser.Controller.
  5. Modify the method to not depend on mPagesDialogHandler. context is your activity.

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视图。

  • Use this new WebViewClient for your web views.

    这篇关于在Android模拟器HTTP代理适用于浏览器,而不是为的WebView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

  • 查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆