Met java bug JDK-8055829 in netbeans module
2017-10-08
I am calling below code in netbeans, and it failed to retrieve any cookies from server. If i run these code in standalone, it got no problem. This only happen in Mac, to fix this by adding "CookieHandler.setDefault(null);" , see more detail https://bugs.openjdk.java.net/browse/JDK-8055829
URL u = new URL(url);
URLConnection uc = u.openConnection();
HttpURLConnection connection = (HttpURLConnection) uc;
connection.setDoOutput(true);
connection.setDoInput(true);
connection.setRequestMethod("POST");
connection.addRequestProperty("Accept", "application/x-www-form-urlencoded");
//connection.addRequestProperty("User-Agent", "Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Win64; x64; Trident/5.0)");
connection.addRequestProperty("Content-Type", "text/xml; charset=utf-8");
connection.setInstanceFollowRedirects(false);
OutputStream out = connection.getOutputStream();
Writer writer = new OutputStreamWriter(out);
logger.info("token3=" + token);
writer.write(token);
writer.flush();
out.flush();
writer.close();
out.close();
String rtFa = null;
String fedAuth = null;
Map < String, List < String >> headerFields = connection.getHeaderFields();
List < String > cookiesHeader = headerFields.get("Set-Cookie");
if (cookiesHeader != null) {
for (String cookie: cookiesHeader) {
logger.info("c=" + cookie);
if (cookie.startsWith("rtFa=")) {
rtFa = "rtFa=" + HttpCookie.parse(cookie).get(0).getValue();
} else if (cookie.startsWith("FedAuth=")) {
fedAuth = "FedAuth=" + HttpCookie.parse(cookie).get(0).getValue();
} else {
logger.info("waste=" + HttpCookie.parse(cookie).get(0).getValue());
}
}
}