Search code examples
javacookiesjava-web-start

Java web start application failed to retrieve cookie value of Set-Cookie


I've encountered an session related issue with a Java Web Start application.

The java web start application will connect to the server through http (not https) with URLConnection java class.

Once connection is established, I found the following log from java console

network: Server http://x.x.x.x:xxxx/xxxx/xx/xxx requesting to set-cookie with   
    "JSESSIONID=xxxxxxxxxxxxxxx"; Path=/xxx/; HttpOnly"

In the code, however, the Set-Cookie value is an empty string

URLConnection urlConn = url.openConnection();

urlConn.setUseCaches(false);
urlConn.setDoOutput(true);
urlConn.setDoInput(true);

urlConn.setRequestProperty("Content-Type", "application/java-serialized-object");

ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(urlConn.getOutputStream()));

out.writeObject(input);
out.flush();
out.close();

String cookieval = urlConn.getHeaderField("Set-Cookie");

if(cookieval == null)
    System.out.println("Set-Cookie is null");   
else if("".equals(cookieval))
    System.out.println("Set-Cookie is empty");

Console output:

Set-Cookie is empty

JRE used by Java web start is 1.7.0.67

Any idea how can I retrieve the Set-Cookie value?


Solution

  • Ok just managed to find the answer. It is fixed by adding following line

    java.net.CookieHandler.setDefault(null);
    

    Reference:

    Session cookie not send back by client using URLConnection in windows with JRE 1.7

    Hope this help someone.