【问题标题】:Android WebView Request header too large exceptionAndroid WebView 请求标头太大异常
【发布时间】:2018-09-30 19:13:25
【问题描述】:

在 webview 中加载某些页面时,几次后我收到此异常 Request Header Or Session Cookie Too Large 显示在 WebView 中。

WebView中URL的加载方式:

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 上遇到过类似的问题?

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

请求标头:{X-Requested-With=ch.denner.inhouse, Cookie=store=de; ls_mage-翻译-存储=%7B%7D; ss_94680522_mage-翻译-存储=%7B%7D;法师翻译存储=%7B%7D; ss_94680522_mage-translation-file-version=%7B%7D;法师翻译文件版本=%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-invalidation=%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-translation-file-version=%7B%7D; ss_13756572_mage-cache-storage=%7B%7D; ss_13756572_mage-cache-storage-section-invalidation=%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-translation-file-version=%7B%7D; ss_69184443_mage-cache-storage=%7B%7D; ss_69184443_mage-cache-storage-section-invalidation=%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-translation-file-version=%7B%7D; ss_11203415_mage-cache-storage=%7B%7D; ss_11203415_mage-cache-storage-section-invalidation=%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-translation-file-version=%7B%7D; ss_51744436_mage-cache-storage=%7B%7D; ss_51744436_mage-cache-storage-section-invalidation=%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-translation-file-version=%7B%7D; ss_98351072_mage-cache-storage=%7B%7D; ss_98351072_mage-cache-storage-section-invalidation=%7B%7D; ls_private_content_version=a34cb889d50dcc9485592bb349ad209b; ls_mage-cache-storage-section-invalidation=%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%22%2C%22isGuestCheckoutAllowed%22%3Atrue%2C%22website_id%22%3A%221%22%2C%22subtotal_incl_tax%22%3A%22%3Cspan%20class%3D%5C%22price%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%22customer%22%3A%7B%22data_id%22%3A1523434855%7D%2C%

【问题讨论】:

  • 你是在使用 CookieManager 来设置 cookie 吗?如果是的话,你能不能也分享一下相同的代码!
  • 嗨@ShubhamSrivastava,不,我没有在Android上使用任何CookieManager,用于加载URL的所有代码都在这篇文章中
  • @ShubhamSrivastava 有任何反馈吗?加载任何其他 URL 时,我没有收到类似的错误,只是在客户提供给我们的这个特定网站上。

标签: android http-headers android-webview


【解决方案1】:

解决方案

我将以下代码添加到我的自定义 WebView(您可以将其添加到任何您想要的位置)。

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()

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-10-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-11-09
    • 2020-10-29
    • 1970-01-01
    • 2021-05-16
    相关资源
    最近更新 更多