Android WebView 请求标头太大异常



在Web视图中加载某些页面时,几次后我收到此异常 请求标头或会话cookie太大显示在WebView中。

URL 在 Web 视图中的加载方式:

webView = (WebView) findViewById(R.id.idWebView);
webView.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
webView.getSettings().setJavaScriptEnabled(true);
webView.setWebViewClient(new WebViewClient());
Map<String, String> headers = new HashMap<>();
headers.put("Accept-Language", getLanguage());
webView.loadUrl(url, headers);

需要明确的是,即使没有headers.put("Accept-Language", getLanguage()),也会发生这种情况;

我不确定这是否应该在Android或服务器端修复。 有没有人在Android WebViews上遇到过类似的问题?

这是此异常的控制台日志:

request header: {X-Request-With=ch.denner.inhouse, Cookie=store=de; ls_mage-translation-storage=%7B%7D; ss_94680522_mage-translation-storage=%7B%7D; mage-translation-storage=%7B%7D; ss_94680522_mage-translation-file-version=%7B%7D; mage-translation-file-version=%7B%

7D; ls_mage-translation-file-version=%7B%22version%22%3A%224a03928408a7b94f82c301d4277eff8aee6c47f7%22%7D;ss_94680522_mage-cache-storage=%7B%7D; ss_94680522_mage-cache-storage-section-invalid=%7B%7D;ls_mage-cache-timeout=%222018-04-11T08%3A55%3A05.513Z%22;ls_recently_viewed_product=%7B%7D;ss_94680522_recently_viewed_product=%7B%7D;ls_recently_viewed_product_previous=%7B%7D;ss_94680522_recently_viewed_product_previous=%7B%7D;ls_recently_compared_product=%7B%7D;ss_94680522_recently_compared_product=%7B%7D;ls_recently_compared_product_previous=%7B%7D;ss_94680522_recently_compared_product_previous=%7B%7D;ls_product_data_storage=%7B%7D;ss_94680522_product_data_storage=%7B%7D;ss_13756572_mage-翻译-存储=%7B%7D;ss_13756572_mage-翻译文件版本=%7B%7D;ss_13756572_mage-cache-storage=%7B%7D;ss_13756572_mage-cache-storage-section-invalid=%7B%7D;ss_13756572_recently_viewed_product=%7B%7D;ss_13756572_recently_viewed_product_previous=%7B%7D;ss_13756572_recently_compared_product=%7B%7D;ss_13756572_recently_compared_product_previous=%7B%7D;ss_13756572_product_data_storage=%7B%7D;ss_69184443_mage-翻译-存储=%7B%7D;ss_69184443_mage-翻译文件版本=%7B%7D;ss_69184443_mage-cache-storage=%7B%7D;ss_69184443_mage-cache-storage-section-invalid=%7B%7D;ss_69184443_recently_viewed_product=%7B%7D;ss_69184443_recently_viewed_product_previous=%7B%7D;ss_69184443_recently_compared_product=%7B%7D;ss_69184443_recently_compared_product_previous=%7B%7D;ss_69184443_product_data_storage=%7B%7D;ss_11203415_mage-翻译-存储=%7B%7D;ss_11203415_mage-翻译文件版本=%7B%7D;ss_11203415_mage-cache-storage=%7B%7D;ss_11203415_mage-cache-storage-section-invalid=%7B%7D;ss_11203415_recently_viewed_product=%7B%7D;ss_11203415_recently_viewed_product_previous=%7B%7D;ss_11203415_recently_compared_product=%7B%7D;ss_11203415_recently_compared_product_previous=%7B%7D;ss_11203415_product_data_storage=%7B%7D;ss_51744436_mage-翻译-存储=%7B%7D;ss_51744436_mage-翻译文件版本=%7B%7D;ss_51744436_mage-cache-storage=%7B%7D;ss_51744436_mage-cache-storage-section-invalid=%7B%7D;ss_51744436_recently_viewed_product=%7B%7D;ss_51744436_recently_viewed_product_previous=%7B%7D;ss_51744436_recently_compared_product=%7B%7D;ss_51744436_recently_compared_product_previous=%7B%7D;ss_51744436_product_data_storage=%7B%7D;ss_98351072_mage-翻译-存储=%7B%7D;ss_98351072_mage-翻译文件版本=%7B%7D;ss_98351072_mage-cache-storage=%7B%7D;ss_98351072_mage-cache-storage-section-invalid=%7B%7D;ls_private_content_version=a34cb889d50dcc9485592bb349ad209b;ls_mage-cache-storage-section-invalid=%7B%7D;ss_98351072_recently_viewed_product=%7B%7D;ss_98351072_recently_viewed_product_previous=%7B%7D;ss_98351072_recently_compared_product=%7B%7D;ss_98351072_recently_compared_product_previous=%7B%7D;ss_98351072_product_data_storage=%7B%7D;ls_mage-cache-storage=%7B%22cart%22%3A%7B%22summary_count%22%3A0%2C%22subtotalAmount%22%3Anull%2C%22subtotal%22%3A%22%3Cspan%20class%3D%5C%22price%5C%22%3EFr.%20%C2%A00.00%3C%2Fspan%3E%22%2C%22possible_onepage_checkout%22%3Atrue%2C%22items%22%3A%5B%5D%2C%22extra_actions%22%3A%22%2C%22isGuestCheckoutAllowed%22%3Atrue%2C%22website_id%22%3A%221%22%2C%22subtotal_incl_tax%22%3A%22%3Cspan%20class%3D%5C%22价格%5C%22%3EFr.%20%C2%A00.00%3C%2Fspan%3E%22%2C%22subtotal_excl_tax%22%3A%22%3Cspan%20class%3D%5C%22price%5C%22%3EFr.%20%C2%A00.00%3C%2Fspan%3E%22%2C%22denner_total_bottles%22%3A0%2C%22denner_subtotal_catalog_discount%22%3A0%2C%22data_id%22%3A1523434856%7D%2C%22customweb_external_checkout_widgets%22%3A%7B%22html%22%3A%5B%5D%2C%22data_id%22%3A1523434855%7D%2C%22%3A%7B%22data_id%22%3A1523434855%7D%2C%

解决方案

我将以下代码添加到我的自定义 Web 视图中(您可以将其添加到所需的任何位置)。

private void clearCookies() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP_MR1) {
CookieManager.getInstance().removeAllCookies(null);
CookieManager.getInstance().flush();
return;
}
CookieSyncManager cookieSyncManager = CookieSyncManager.createInstance(getContext());
cookieSyncManager.startSync();
CookieManager cookieManager = CookieManager.getInstance();
cookieManager.removeAllCookie();
cookieManager.removeSessionCookie();
cookieSyncManager.stopSync();
cookieSyncManager.sync();
}

所以,在加载任何URL之前,我只需调用clearCookies()

最新更新