4

我正在做一个项目,我们有兴趣从 WebView 拦截一些 HTTP 流量,然后将一些额外的标头附加到请求中,然后返回并在 WebView 中显示它。

为此,我们使用 shouldInterceptRequest(WebView view, String URL) 方法,该方法返回 webview 应该显示的 WebResourceResponse。

这是我所做的:

private WebResourceResponse handleRequest(String url){
    Log.d(TAG, "Intercepting request at : " + url);
    HttpURLConnection connection = null;        
    URL serverAddress = null;           
    WebResourceResponse response = null;
    InputStream is = null;
    String type = null;
    String encoding = null;
    int statusCode;
    int length;

    try
    {
        //Set up the initial connection
        serverAddress = new URL(url);
        connection = (HttpURLConnection)serverAddress.openConnection();

        // Initiate a HEAD request to get meta data
        connection.setRequestMethod("HEAD");

        // Let the WebView handle requests with no headers in them.
        Map<String, List<String>> headers = connection.getHeaderFields();
        if(headers == null){
            Log.d(TAG, "Retrieved response with zero headers at: " + url);
            return null;
        }

        statusCode = connection.getResponseCode();

        if(statusCode == 200){
            // OK, delegate request if content length is large enough
            type = connection.getContentType();
            encoding = connection.getContentEncoding();
            length = connection.getContentLength();

            Log.d(TAG, "ContentType = " + type + " encoding = " + encoding + " " + url);

           connection.disconnect();

            if(length > SIZE_THRESHOLD){
                // send to other device

                Log.d(TAG, "Delegating request to other device NOT IMPLEMENTED");
                // TODO: Currently just handling them locally.
            }
            else{
                connection = (HttpURLConnection)serverAddress.openConnection();
                connection.setRequestMethod("GET");             
                is = connection.getInputStream();
                statusCode = connection.getResponseCode();
                type = connection.getContentType();
                encoding = connection.getContentEncoding();

                Log.d(TAG, "Done Loading " + url);

                return new WebResourceResponse(type, encoding, is);
            }

        }
    } 
    catch (MalformedURLException e) {
            e.printStackTrace();
    }
    catch (ProtocolException e) {
            e.printStackTrace();
    } 
    catch (IOException e) {
        Log.d(TAG, "IOException when connection to : " + url + " Stacktrace: " + Log.getStackTraceString(e));
    }
    return response;
}   

如果我尝试加载像“www.madopskrifter.nu”或“www.newz.dk”这样的页面,那么我会得到整个内容,并显示图片和所有内容。但是,如果我浏览像 stackoverflow 这样的页面,我只会在 stackoverflow.com 上获得 HTML 索引文件。youtube.com 也是如此。(我确实在 WebView 设置中启用了 JavaScript)

有什么我想念的吗?目前我什至没有添加我自己的标题或任何东西,我只是简单地返回相同的响应,除了我“手动”获取它们。(此外,我现在没有处理带有零标头的请求,但即使我注释掉该代码,问题仍然存在)。

任何帮助将不胜感激,

4

1 回答 1

12

我找到了解决我遇到的同样问题的方法,YMMV:

我的 HttpUrlConnection 正在返回:

  • 编码:空
  • mimeType:“文本/html;字符集=utf-8”

这就是问题所在。

我现在检查 html 文件,并更改为:

  • 编码:“UTF-8”
  • mimeType:“文本/html”

例子:

String responseEncoding = urlConnection.getContentEncoding();
String responseMimeType = urlConnection.getContentType();

String html = "";
if (resourceUrl.endsWith("html"))
{
    responseEncoding = "UTF-8";
    responseMimeType = "text/html";
}

if((responseCode == 200) && (!is.equals("")))
{
    return new WebResourceResponse(responseMimeType, responseEncoding, returnStream); 
} else
{
    return null;
}
于 2013-06-05T17:51:28.293 回答