【问题标题】:Android WebViewClient url redirection (Android URL loading system)Android WebViewClient url 重定向(Android URL 加载系统)
【发布时间】:2013-08-16 17:50:08
【问题描述】:

我正在尝试使用ShouldInterceptRequest 拦截 webview 请求,在其中我使用 HttpUrlConnection 从服务器获取数据,我将其设置为遵循重定向,这对 webviewclient 是透明的。这意味着当我返回 WebResponseResource("", "", data_inputstream) 时,webview 可能不知道目标主机已更改。我怎样才能告诉 webview 这发生了?

ourBrowser.setWebViewClient(new WebViewClient() {
        @Override
        public WebResourceResponse shouldInterceptRequest(WebView view,
                String url) {
                    ..... //some code omitted here

                HttpURLConnection conn = null;
                try {
                    conn = (HttpURLConnection) newUrl.openConnection();
                    conn.setFollowRedirects(true);
                } catch (IOException e) {
                    e.printStackTrace();
                }
                   ..... //

                InputStream is = null;
                try {
                    is = conn.getInputStream();
                } catch (IOException e) {
                    e.printStackTrace();
                }
                return new WebResourceResponse(mimeType, encoding, is);

            }
        }

如果我请求“google.com”,它应该被重定向到“google.co.uk”,但webview不知道重定向,如果附加“co.uk”的css文件的链接是“/render/ something.css",webview 仍然去 "http://www.google.com/render/something.css" 找到应该是 "http://www.google.co.uk/render/something.css" 的 css 文件。

谁能帮帮我?

【问题讨论】:

    标签: android redirect webview webviewclient android-webview


    【解决方案1】:

    WebResourceResponse 类无法指定某些元数据(网址、标题等)。这意味着您只能使用 shouldInterceptRequest 提供不同的数据(更改页面内容),但不能使用它来更改正在加载的 URL。

    在您的情况下,您正在使用 HttpUrlConnection 中的重定向,因此 WebView 仍然认为它正在加载“http://www.google.com/”(即使内容来自“http://google.co.uk/”)。如果 Google 的主页没有明确设置基本 URL,WebView 将继续假定基本 URL 是“http://www.google.com/”(因为它没有看到重定向)。由于相对资源引用(如<link href="//render/something.css" />)是针对baseURL(在本例中为“http://www.google.com/”而不是“http://www.google.co.uk/”)解析的,因此您会得到观察到的结果。

    您可以做的是使用HttpUrlConnection 来确定您要加载的URL 是否是重定向并在这种情况下返回null。但是,我强烈建议不要在一般情况下使用shouldInterceptRequest 中的HttpUrlConnection - WebView 的网络堆栈效率更高,并且将并行执行提取(而使用shouldInterceptRequest 将序列化所有预 KK WebViews 中的负载)。

    【讨论】:

      【解决方案2】:

      关闭 HttpClient setFollowRedirects(false) 并让 webiew 重新加载重定向的 URL。

      假码:

      if(response.code == 302){
        webview.load(response.head("location"));
      }
      

      【讨论】:

        【解决方案3】:

        您可以为所有 HttpURLConnection 对象全局启用 HTTP 重定向:

        HttpURLConnection.setFollowRedirects(true);
        

        然后在 shouldInterceptRequest() 方法中检查连接响应代码:

        public WebResourceResponse shouldInterceptRequest(WebView view, String url) {
          ...
          int respCode = conn.getResponseCode();
          if( respCode >= 300 && respCode < 400 ) {
            // redirect
            return null;
          } else if( respCode >= 200 && respCode < 300 ) {
            // normal processing
            ...
        }
        

        Android 框架应该再次调用 shouldInterceptRequest() 并使用作为重定向目标的新 URL,这次连接响应代码将为 2xx。

        【讨论】:

        • 您需要禁用“followRedirects”而不是启用(默认情况下启用)。启用 followRedirects 后,您将永远不会收到 3xx 代码。
        猜你喜欢
        • 2017-06-27
        • 2014-04-14
        • 2011-11-21
        • 2012-01-07
        • 2014-04-13
        • 1970-01-01
        • 1970-01-01
        • 2017-03-06
        相关资源
        最近更新 更多