2016-03-06 60 views
0

我有一個爲localhost:9090/application創建cookie的方法。使用URLConnection的Java cookie

public Object makeCookie(String p) throws IOException{ 

     URL myUrl = new URL("localhost:9090/application"); 
     URLConnection urlConn = myUrl.openConnection(); 

     urlConn.setRequestProperty("testCookie", p); 
     urlConn.connect(); 

     return urlConn; 
    } 

我有一個方法,應該打印我剛剛設置的cookie的名稱和域,但我沒有得到任何結果。

CookieManager cookieManager; 
    URL url; 
    URLConnection connection; 
    CookieStore cookieStore; 
    List<HttpCookie> cookieList; 
public boolean checkone (String test1) throws ClassNotFoundException, IOException{ 
     cookieManager = new CookieManager(); 
     CookieHandler.setDefault(cookieManager); 
     url = new URL("localhost:9090/application/"); 
     connection = url.openConnection(); 
     connection.getContent(); 
     cookieStore = cookieManager.getCookieStore(); 

     cookieList = cookieStore.getCookies(); 
     for (HttpCookie cookie: cookieList){ 

      System.out.println("Domain: " + cookie.getDomain()); 
      System.out.println("name of cookie: " + cookie.getName()); 

     } 
     return true;  

    } 

我在創建cookie時丟失了什麼嗎?

回答