String cookieValue = urlConn.getHeaderField("Set-Cookie");
Map header = urlConn.getHeaderFields();
System.out.println("header "+header);
System.out.println("JSESSIONID "+urlConn.getHeaderField("Set-Cookie"));
header 里有JSESSIONID,但是第二个打印里没哟JSESSIONID
调试结果和源码注解指出,getHeaderField : If called on a connection that sets the same header multiple times
* with possibly different values, only the last value is returned.
getHeaderFields 的返回值类型 Map<String,List<String>> ,下面贴出这个map
说明 如果Map<String,List<String>> 的 list的size大于1,那么getHeaderField拿到 list.get(0)
所以获取方法为:
Map header = urlConn.getHeaderFields();
JSESSIONID = ((List<String>)header.get("Set-Cookie")).get(1);
网友评论