簡體   English   中英

從MessageContext.HTTP_RESPONSE_HEADERS獲取Cookie值

[英]Getting Cookie value from MessageContext.HTTP_RESPONSE_HEADERS

如何從cookie列表中獲取特定cookie的值。 以下是我的嘗試方式:

        Map<String, List<String>> map = (Map<String, 
                List<String>>) context.get(MessageContext.HTTP_RESPONSE_HEADERS);

        List<String> contentType = getHTTPHeader(map);
        if (contentType != null) {
            StringBuffer strBuf = new StringBuffer();
            for (String type : contentType) {
                strBuf.append(type);
            }
            System.out.println("Content-Type:" + strBuf.toString());
        }

        List<String> cookies = map.get("Set-Cookie"); 
        if (cookies != null) {
            System.out.println("cookies != null");
            StringBuffer strBuf = new StringBuffer();
            for (String type : cookies) {
                System.out.println(" Looping cookie ");
                strBuf.append(type);
            }
            System.out.println("Cookie:" + strBuf.toString());
        }else{
            System.out.println("cookies == null");
        }

我得到以下結果,我想掌握“ JSESSIONID”的價值

Cookie:JSESSIONID=88E53DE2E78TRE86E1C2B021BA240B; Path=/us-webservice

謝謝。

cookie.substring(cookie.indexOf(':'), cookie.indexOf(';')).split("=")[1]

或者如果您想要正則表達式

Pattern p = Pattern.compile( "JSESSIONID=([A-Z0-9]+)");
Matcher m = p.matcher(cookie);
m.find();
m.group(1);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM