根据 Cookie 规范,Cookie 的 expires
属性的格式应类似于 Expires=Wed, 09 Jun 2021 10:18:14 GMT
。
我使用rest-assured通过请求发送cookie,但到期日期的格式如下:Expires=9/4/18 12:03 PM
我使用rest-assured的Cookie.Builder.setExpiryDate()
来创建cookie,它只接受java Date对象作为输入。
有什么方法可以让我放心地更改到期日期的格式以符合 cookie 规范吗?
最佳答案
没有。因为他们使用以下代码
final SimpleDateFormat simpleDateFormat = new SimpleDateFormat();
simpleDateFormat.setTimeZone(TimeZone.getTimeZone("GMT"));
builder.append(COOKIE_ATTRIBUTE_SEPARATOR).append(EXPIRES).append(EQUALS).append(simpleDateFormat.format(expiryDate));
所以,你会得到等价于这个
Date d1 = new Date();
System.out.println(d1);
SimpleDateFormat simpleDateFormat = new SimpleDateFormat();
simpleDateFormat.setTimeZone(TimeZone.getTimeZone("GMT"));
System.out.println(simpleDateFormat.format(d1));
输出类似于“7/10/19, 5:50 PM”。
您可以获取该值,然后在最后进行格式化。
关于java - 放心发送过期日期格式错误的cookie吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52161410/