2010-02-10 14 views

cevap

22
public static void main(String[] args) throws Exception { 
     LogFactory.getFactory().setAttribute("org.apache.commons.logging.Log", "org.apache.commons.logging.impl.NoOpLog"); 

     File file = new File("cookie.file"); 
     ObjectInputStream in = new ObjectInputStream(new FileInputStream(file)); 
     Set<Cookie> cookies = (Set<Cookie>) in.readObject(); 
     in.close(); 

     WebClient wc = new WebClient(); 

     Iterator<Cookie> i = cookies.iterator(); 
     while (i.hasNext()) { 
      wc.getCookieManager().addCookie(i.next()); 
     } 

     HtmlPage p = wc.getPage("http://google.com"); 

     ObjectOutput out = new ObjectOutputStream(new FileOutputStream("cookie.file")); 
     out.writeObject(wc.getCookieManager().getCookies()); 
     out.close(); 
    } 
+1

Ben daha fazla insan sizin gibi yorumladı diliyorum. Basit. Soruyu cevaplar. Parlak. Çok teşekkür ederim! – David

+0

htmlunit 2.23 'wc.getCookieManager(). AddCookie (i.next());' bir sonraki istekte Çerezlerin gönderilmesine neden olmadı (_com.gargoylesoftware.htmlunit.Webclient_ ve _com.gargoylesoftware kodunun kontrol edildi. htmlunit.WebRequest_, CookieManager orada kullanılmamaktadır). Çerezleri yeni bir İstekle (ör. WebRequest'i manuel olarak oluşturma) geçmek için yalnızca [bu yanıtı] kullanabilirim (http://stackoverflow.com/a/36155793/2039709). – user2039709

3

Yukarıdaki kod HtmlUnit'in yani ihracat sadece HtmlUnit'in sözümüze tarafından okunabilir bir formatta (Im eleştiren ya da bir şey değil) ile çalışır. İşte

bir daha genel bağlıdır: (Bu kıvrılma ile çalışır)

CookieManager CM = WC.getCookieManager(); //WC = Your WebClient's name 
    Set<Cookie> set = CM.getCookies(); 
    for(Cookie tempck : set) { 
     System.out.println("Set-Cookie: " + tempck.getName()+"="+tempck.getValue() + "; " + "path=" + tempck.getPath() + ";"); 
    } 

Şimdi, döngü bu println (ler) dışında Dize olun. Onları bir metin dosyasına yaz. Kıvrık

çalışır:

curl -b "path to the text file" "website you want to visit using the cookie" 

-b çok .. bukle dokümanlar kontrol -c ile değiştirilebilir ...

İlgili konular