Java Web Start应用程序无法检索Set-Cookie的cookie值

时间:2014-09-11 04:57:55

标签: java cookies java-web-start

我遇到了与Java Web Start应用程序有关的会话问题。

java web start应用程序将通过带有URLConnection java类的http(而不是https)连接到服务器。

建立连接后,我从java控制台找到了以下日志

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

但是,在代码中,Set-Cookie值为空字符串

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");

控制台输出:

Set-Cookie is empty

Java web start使用的JRE是1.7.0.67

任何想法如何检索Set-Cookie值?

2 个答案:

答案 0 :(得分:0)

好的,我找到了答案。它通过添加以下行来修复

java.net.CookieHandler.setDefault(null);

参考:

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

希望这有助于某人。

答案 1 :(得分:-1)

标题中的HttpOnly标志必须取消设置。