Java 从MessageContext.HTTP\u响应\u头获取Cookie值
如何从cookie列表中获取特定cookie的值。下面是我如何尝试的:Java 从MessageContext.HTTP\u响应\u头获取Cookie值,java,cookies,http-headers,Java,Cookies,Http Headers,如何从cookie列表中获取特定cookie的值。下面是我如何尝试的: Map<String, List<String>> map = (Map<String, List<String>>) context.get(MessageContext.HTTP_RESPONSE_HEADERS); List<String> contentType = getHTTPHeade
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");
}
谢谢。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);
你不能从会话本身获取会话id吗?你是什么意思?它已被设置为cookie的一部分,我必须在下一个请求中阅读并传递它。如果您提出请求,像httpclient这样的客户端可以自动为您处理这些cookie。
Pattern p = Pattern.compile( "JSESSIONID=([A-Z0-9]+)");
Matcher m = p.matcher(cookie);
m.find();
m.group(1);