我使用Apache Http組件爲本地主機設置cookie。當我返回cookie我得到的輸出:Apache Http組件 - 設置Cookie
[version: 0][name: testCookie][value: test][domain: http://localhost:9090/][path: /][expiry: Mon Aug 07 19:11:56 BST 2017]
這讓我想起了餅乾已定,但是當我在Chrome中檢查,看它是否在Chrome://設置/餅乾有什麼也沒有。
public Object makeCookie(String p) throws IOException, MalformedCookieException{
Calendar myCal = Calendar.getInstance();
myCal.set(2017, 07, 07);
Date theDate = myCal.getTime();
CookieStore cookieStore = new BasicCookieStore();
BasicClientCookie cookie = new BasicClientCookie("testCookie",p);
// Set effective domain and path attributes
cookie.setDomain("http://localhost:9090/");
cookie.setPath("/");
cookie.setExpiryDate(theDate);
cookieStore.addCookie(cookie);
// Set attributes exactly as sent by the server
cookie.setAttribute(ClientCookie.PATH_ATTR, "/");
cookie.setAttribute(ClientCookie.DOMAIN_ATTR, "http://localhost:9090/");
CloseableHttpClient httpclient = HttpClients.custom()
.setDefaultCookieStore(cookieStore)
.build();
return cookie;
}
我一直停留在這幾個小時,我只是不明白,爲什麼它沒有被存儲在瀏覽器
感謝您的回覆。問題是,我正在使用nanohttp(一個lightwight服務器),誰的cookie不提供設置路徑的方法。這就是爲什麼我不想使用servlet API並試圖找到另一種通過cookie的方法 – Vickie