美文网首页
Java:HttpURLConnection 获取不到 JSES

Java:HttpURLConnection 获取不到 JSES

作者: X4tar | 来源:发表于2020-02-08 21:22 被阅读0次
    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);
    

    相关文章

      网友评论

          本文标题:Java:HttpURLConnection 获取不到 JSES

          本文链接:https://www.haomeiwen.com/subject/nyrwxhtx.html