Java:HttpURLConnection 获取不到 JSES

2020-02-08  本文已影响0人  X4tar
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);
上一篇下一篇

猜你喜欢

热点阅读