Android(4.4+)WebView忽略x-frame-options

时间:2016-05-18 08:22:01

标签: java android iframe webview x-frame-options

我正在尝试在应用中显示带有iframe的页面。它显示简单的页面,但无法显示googlemap iframe。

我试过了: hardvre accelleration - 打开(因为我知道它只与视频有关), java脚本 - 启用, 使用web chrome客户端, loadurl(因为从内部存储器加载),等等

代码是:

        wewview = new WebView(getActivity().getApplicationContext());
        wewview.getSettings().setJavaScriptEnabled(true);
        wewview.getSettings().setLoadWithOverviewMode(true);  
        wewview.getSettings().setUseWideViewPort(true);
        wewview.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,  
                LayoutParams.MATCH_PARENT));
        wewview.setWebChromeClient(new WebChromeClient());
        if (Build.VERSION.SDK_INT >= VERSION_CODES.JELLY_BEAN) {
            wewview.getSettings().setAllowUniversalAccessFromFileURLs(true);
        }
        wewview.getSettings().setPluginState(WebSettings.PluginState.ON);
        wewview.getSettings().setPluginState(WebSettings.PluginState.ON_DEMAND); 

        linearMain.addView(wewview,0);  

        wewview.loadUrl("file://path... /frames.html");

有人可以建议我如何以编程方式为我的webview打开“忽略x-frame-options”?

更新1

在日志中找到了另一条消息

05-19 10:01:49.404: I/chromium(4027): [INFO:CONSOLE(0)] "Refused to display 'https://www.google.ru/maps/@?nogmmr=1' in a frame because it set 'X-Frame-Options' to 'SAMEORIGIN'.", source: about:blank (0)

寻找忽略x-frame-options

1 个答案:

答案 0 :(得分:1)

找到了方法。加载的框架上没有完全交互,但我只需要显示它。所以,Jsoup用于加载页面内容,但是继续以标准方式加载脚本和图像,这就是所有技巧:

   private class ignoreXHeaderWebClient extends WebViewClient{  
   private static final String URL_MASK       =  "http";


    //api < 21
    @Override
    public WebResourceResponse shouldInterceptRequest (final WebView view, String url) {
        WebResourceResponse content = getsPageContent(url);
        if (content != null) {
            return content;
        } else {
            return super.shouldInterceptRequest(view, url);
        }
    }

   // api >= 21
    @Override  
    public WebResourceResponse shouldInterceptRequest(WebView view, WebResourceRequest request) {
        WebResourceResponse content = getsPageContent(request.getUrl().toString());
        if (content != null) {
            return content;
        } else {
            return super.shouldInterceptRequest(view, request);  
        }
    }   

    private WebResourceResponse getsPageContent(String url){
        if (( url.contains(URL_MASK)) // guess is better to create for ex map to check all extensions
                && !(( url.contains(".js")) 
                      ||( url.contains(".css"))
                      ||( url.contains(".tiff"))
                      ||( url.contains(".jpg"))
                      ||( url.contains(".png"))
                      ||( url.contains(".gif")))){    
            try {  
                return new WebResourceResponse(
                              "text/html",    
                              "UTF-8",   
                              new ByteArrayInputStream( 
                                   Jsoup.connect(url)
                                           .get()  
                                           .toString()
                                           .getBytes()));  
            } catch (IOException e) {
                e.printStackTrace();    
            }   
        }
        return null;
    } 
}

并申请新课程:

webWiev.setWebViewClient(ignoreXHeaderWebClient);