Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9189439
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T20:14:14+00:00 2026-06-17T20:14:14+00:00

I cannot seem to find any way to set a cookie programatically using WebEngine

  • 0

I cannot seem to find any way to set a cookie programatically using WebEngine / WebView in JavaFX. The API doesn’t give any idea as to how to obtain an HttpRequest-like object to modify the headers (which is what I use in the app for XML-RPC), or any sort of cookie manager.

No questions on this page seem to touch on the issue either – there is this but it just disables cookies when in applet to fix a bug, my app is on desktop btw.

The only way I image I could do it is by requesting the first page (which requires a cookie with a sessionID to load properly), getting an “access denied”-style message, executing some javascript in the page context which sets the cookie and then refreshing. This solution would be a horrible user experience though.

How do I set a cookie using WebEngine?


Update: Taking a clue from a question linked above, I tried digging around for some examples of using CookieManager and related APIs. I found this code, which I then tried to incorporate into my app, with weird results;

MyCookieStore cookie_store = new MyCookieStore();
CookieManager cookie_manager = new CookieManager(cookie_store, new MyCookiePolicy());
CookieHandler.setDefault(cookie_manager);
WebView wv = new WebView();

Now lets say we do this:

String url = "http://www.google.com/";
wv.getEngine.go(url);

Debugging in Eclipse after this request has been made shows that the cookie store map holds a cookie:

{http://www.google.com/=[NID=67=XWOQNK5VeRGEIEovNQhKsQZ5-laDaFXkzHci_uEI_UrFFkq_1d6kC-4Xg7SLSB8ZZVDjTUqJC_ot8vaVfX4ZllJ2SHEYaPnXmbq8NZVotgoQ372eU8NCIa_7X7uGl8GS, PREF=ID=6505d5000db18c8c:FF=0:TM=1358526181:LM=1358526181:S=Nzb5yzBzXiKPLk48]}

THAT IS AWESOME

WebEngine simply uses the underlying registered cookie engine! But wait, is it really? Lets try adding a cookie, prior to making the request…

cookie_store.add(new URL(url).toURI(), new HttpCookie("testCookieKey", "testCookieValue"));

Then I look at the request in Wireshark…

GET / HTTP/1.1
Accept-Language: en-us,en;q=0.5
Accept-Encoding: gzip
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7
User-Agent: Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/535.14 (KHTML, like Gecko) JavaFX/2.2 Safari/535.14
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Cache-Control: no-cache
Pragma: no-cache
Host: www.google.com
Connection: keep-alive

No cookie for me 🙁

What am I doing wrong?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T20:14:15+00:00Added an answer on June 17, 2026 at 8:14 pm

    I have managed to solve this issue with the help of Vasiliy Baranov from Oracle. Vasiliy wrote to me:

    Try putting the cookie into java.net.CookieHandler.getDefault() after
    the WebView is instantiated for the first time and before the call to
    WebEngine.load, e.g. as follows:

    WebView webView = new WebView();
    URI uri = URI.create("http://mysite.com");
    Map<String, List<String>> headers = new LinkedHashMap<String, List<String>>();
    headers.put("Set-Cookie", Arrays.asList("name=value"));
    java.net.CookieHandler.getDefault().put(uri, headers);
    webView.getEngine().load("http://mysite.com");
    

    This will place the cookie into the store permanently, it should be sent out on every subsequent request (presumably provided that the server doesn’t unset it).

    Vasiliy also explained that WebView will install it’s own implementation of the CookieHandler, while retaining cookies put into the default one.

    Lastly, he mentions something quite intriguing:

    Do not waste your time trying to use java.net.CookieManager, and
    java.net.CookieStore. They are likely to cause problems with many
    sites because they implement the wrong standard.

    I tried googling after this but it doesn’t seem to be common knowledge. If anyone is able to provide more details I would be grateful. It seems weird, since it seems CookieStore and CookieManager are used by a lot of software out there.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

All, I'm developing a dnn 6 module and cannot seem to find any way
I cannot seem to find a way to have LINQ return the value from
I am stuck on this issue and cannot seem to find a way around
I can't seem to find any way to add a horizontal separator in a
I cannot seem to find any documentation of what events fire and when in
I cannot seem to find any concrete answers for this. I have a client
i cannot seem to find the answer to this. i uploaded code to pastebin
I cannot seem to find the answer to this I am looking for a
I researched this question for days and cannot seem to find an option I
I have been researching this issue pretty extensively and cannot seem to find an

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.