Click here to Skip to main content
15,887,413 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I want to develop an app which write some value in cookies (and update periodically), and on web page (using webview) there is a button, by clicking the button it will show the cookie value. I found lots of solution, but none of them solve my issue. I am not getting require cookie value.

Thanks in advance.

What I have tried:

On My App:
public class MainActivity extends Activity 
{
  private Handler MyHandler = new Handler();

  protected void onCreate(Bundle savedInstanceState) 
  {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        WebView  wv1 =( WebView)findViewById(R.id.webView1);
        wv1.getSettings().setLoadsImagesAutomatically(true);
        wv1.getSettings().setJavaScriptEnabled(true);
        String url = "https://abcd.com";          
        wv1.loadUrl(url);
        MyHandler .postDelayed(MyThread, 0);
        CookieManager.getInstance().setAcceptThirdPartyCookies(wv1, true);    
  }

  private Runnable MyThread = new Runnable() 
  {

        public void run() 
        {
            Update_Value(); 

            MyHandler.postDelayed(this, 10000);
        }

  };    

  private void Update_Value()
  {
    String _Value= Get_Value();

    CookieSyncManager.createInstance(this);
    CookieManager cookieManager = CookieManager.getInstance();
    cookieManager.setAcceptCookie(true);
    cookieManager.setCookie("MyValue", _Value);
    CookieSyncManager.getInstance().sync();
    //?cookieManager.flush();
  }  
}


On Web Page - https://abcd.com/default.aspx (ASP.Net)
protected void btn_ShowValue_Click(object sender, EventArgs e)
{
  object _Value = Response.Cookies["MyValue"].Value;

  if (_Value != null) txt_Result.Text = "Result : " + _Value.ToString();
  else txt_Result.Text = "Value not found";
}
Posted
Updated 13-May-20 0:00am
Comments
Richard Deeming 12-May-20 15:50pm    
Try Request.Cookies instead of Response.Cookies.

If that doesn't work, you'll need to debug your code to see why the cookie isn't being set.
Subrata.In 12-May-20 22:52pm    
Thank you so much. It solve my problem.

1 solution

As mentioned in the comments, you need to use the Request.Cookies collection, not the Response.Cookies collection.

The request cookies[^] are the ones sent by the client to the server. The response cookies[^] are the new ones that will be sent back to the client.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900