【问题标题】:HTML cookies not working with android webviewHTML cookie 不适用于 android webview
【发布时间】:2016-07-09 13:12:23
【问题描述】:

我正在为我的 asp.net Web 应用程序使用 android webview,下面的代码假设更改所选的国家/地区,这在 web 浏览器中运行良好,但当我单击此按钮时,它不能与 webview 中的 android webview 一起使用它移动我到 index.aspx 但国家仍然是在他没有接受 cookie 的新值之前选择的国家/地区

protected void chgcontrybtn_Click(object sender, EventArgs e)
    {
        if (Request.Cookies["countryCookies"] != null)
        {
            Response.Cookies["countryCookies"].Expires = DateTime.Now.AddDays(-1);//to remove cookies

            Response.Redirect("Index.aspx");
        }

        else if (Request.Cookies["Location"] != null)
        {
            Response.Cookies["Location"].Expires = DateTime.Now.AddDays(-1);

            Response.Redirect("Index.aspx");
        }

    }

【问题讨论】:

    标签: c# android cookies webview


    【解决方案1】:

    在 android 端,您必须启用 cookie,在初始化您的 webview 之前

    CookieManager.getInstance().setAcceptCookie(true);
    

    CookieManager.getInstance() 是整个应用程序的 CookieManager 实例。然后,您将为应用程序中的所有网络视图启用 cookie。

    【讨论】:

    • 感谢您的回复,此行已添加,但问题是它需要时间才能更改并接受新的 cookie 值
    【解决方案2】:

    尝试添加这个..

    if (Build.VERSION.SDK_INT >= "Your SDK VERSION"){
      CookieManager cookieManager = cookieManager.getInstance();
      cookieManager.setAcceptThirdPartyCookies(mWebView, true);
    }
    

    【讨论】:

    • 纯代码答案几乎与纯链接答案一样无用。请说明问题是什么,以及您提出的解决方案为何能解决该问题。
    猜你喜欢
    • 2021-06-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-04-10
    • 1970-01-01
    • 1970-01-01
    • 2012-10-08
    • 2019-08-06
    相关资源
    最近更新 更多