【问题标题】:Android Intercept a WebView request properlyAndroid 正确拦截 WebView 请求
【发布时间】:2017-01-23 12:59:42
【问题描述】:

我目前在 webview 中拦截请求的代码是

 @Override
public WebResourceResponse shouldInterceptRequest(WebView view,
    String url) {
    String ext = MimeTypeMap.getFileExtensionFromUrl(url);
    String mime = MimeTypeMap.getSingleton().getMimeTypeFromExtension(ext);
    if (mime == null) {
        return super.shouldInterceptRequest(view, url);
    } else {
        HttpURLConnection conn = (HttpURLConnection) new URL(
                                                 url).openConnection();
        conn.setRequestProperty("User-Agent", userAgent);
        return new WebResourceResponse(mime, "UTF-8",
                                                 conn.getInputStream());
    }
}

我从 The best way to intercept a WebView request in Android.

但是,每当我尝试执行身份验证时,假设我正在我的 web 视图中加载 facebook。

mWebView.loadUrl("https://www.facebook.com/");

什么都没有发生,我注意到的是,请求标头和响应都不完整。此外,来源中没有 cookie。 (我在通过 Chrome 远程调试 webview 时看到了这一点。

如果我错了,请纠正我,但我认为不完整的标头和丢失的 cookie 是导致登录请求失败的原因。

有没有办法可以修改请求并设置其标头?也为了回应,我也应该这样做吗?最后,我怎样才能拥有 cookie。

【问题讨论】:

标签: android cookies webview


【解决方案1】:

这个问题已经6个月没有回答了,所以我不知道你是否还需要这个,但也许其他人也有类似的问题。

请求标头不完整

使用HttpURLConnection 时,您将负责设置您可能需要的任何请求标头,但这就像设置用户代理一样简单,您已经这样做了:conn.setRequestHeader(header, value) 或者如果您想添加而不是覆盖标头值:conn.addRequestHeader(header, value)

或者,您可以使用okhttp,这是一个 HTTP 客户端,它应该为标头添加默认值,这是通常所期望的。

源中没有 cookie

在拦截请求时,您还将负责处理 cookie。您可以通过解析响应中的标头来手动存储 cookie,例如

    public WebResourceResponse shouldInterceptRequest(WebView view,
       String url) { 
       // do your stuff
       conn.connect(); // required to tell that the connection should be established
       String cookie = getCookieFromConnection(conn);
       // do more stuff and return WebResourceResponse
    }

    /**
     * iterates all headers, and finds "cookie" headers 
     * (there could be more than one)
     * @return cookie (concatenated value of all the found cookies)
     * or null if no cookie has been found
     */
    private String getCookieFromConnection(HttpURLConnection connection) {
       String cookie = "";
       Map<String, List<String>> header = connection.getHeaderFields();
       List<String> cookies = header.get(COOKIE_HEADER);
       if (cookies != null) {
           for (String c : cookies) {
               if (c != null) cookie += c + ";";
           }
       }
       if (cookie.isEmpty()) return null;
       return cookie;
}

或者你可以使用CookieManager,它会为你处理一切:

    cookieManager = new CookieManager();
    CookieHandler.setDefault(cookieManager); 
    cookieManager.setCookiePolicy(CookiePolicy.ACCEPT_ALL);

在使用 okhttp 时,您可能还需要处理您的 cookie,但您也可以使用如上所述的 CookieManager。有关详细信息,请参阅此文档,或此 stackoverflow question

如果我错了,请纠正我,但我认为不完整的标头和丢失的 cookie 是导致登录请求失败的原因。

还有一个问题,当拦截WebView 中的请求时:它以某种方式停止加载和评估javascript。我在网上找到了这个blog by Artem Zinnatullin,他描述了这种行为,我也遇到了同样的行为。

如果有人对此有解决方案,我将非常高兴。

【讨论】:

    猜你喜欢
    • 2018-02-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-02-18
    • 2012-01-06
    • 2015-12-06
    • 2019-02-18
    相关资源
    最近更新 更多