title: Setting a cookie using JavaFX's WebEngine/WebView
body: <p>I cannot seem to find any way to set a cookie programatically using <a href="http://docs.oracle.com/javafx/2/api/javafx/scene/web/WebEngine.html" rel="noreferrer">WebEngine</a> / <a href="http://docs.oracle.com/javafx/2/api/javafx/scene/web/WebView.html" rel="noreferrer">WebView</a> 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.</p>
  
  <p>No questions on this page seem to touch on the issue either - there is <a href="https://stackoverflow.com/questions/11316444/javafx2-0-webview-not-rendering-the-page">this</a> but it just disables cookies when in applet to fix a <a href="http://javafx-jira.kenai.com/browse/RT-15676" rel="noreferrer">bug</a>, my app is on desktop btw.</p>
  
  <p>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.</p>
  
  <p>How do I set a cookie using WebEngine?</p>
  
  <hr>
  
  <p><strong>Update:</strong> Taking a clue from a question linked above, I tried digging around for some examples of using <a href="http://docs.oracle.com/javase/7/docs/api/java/net/CookieManager.html" rel="noreferrer">CookieManager</a> and related APIs. <a href="http://www.java2s.com/Code/Java/JDK-6/UsingCookieStore.htm" rel="noreferrer">I found this code</a>, which I then tried to incorporate into my app, with weird results;</p>
  
  <pre><code>MyCookieStore cookie_store = new MyCookieStore();
  CookieManager cookie_manager = new CookieManager(cookie_store, new MyCookiePolicy());
  CookieHandler.setDefault(cookie_manager);
  WebView wv = new WebView();
  </code></pre>
  
  <p>Now lets say we do this:</p>
  
  <pre><code>String url = "http://www.google.com/";
  wv.getEngine.go(url);
  </code></pre>
  
  <p>Debugging in Eclipse after this request has been made shows that the cookie store map holds a cookie:</p>
  
  <pre><code>{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]}
  </code></pre>
  
  <h2>THAT IS AWESOME</h2>
  
  <p>WebEngine simply uses the underlying registered cookie engine! But wait, is it really? Lets try adding a cookie, prior to making the request...</p>
  
  <pre><code>cookie_store.add(new URL(url).toURI(), new HttpCookie("testCookieKey", "testCookieValue"));
  </code></pre>
  
  <p>Then I look at the request in Wireshark...</p>
  
  <pre><code>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
  </code></pre>
  
  <p>No cookie for me :(</p>
  
  <p>What am I doing wrong?</p>