2010-02-10 6 views

답변

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

나는 더 많은 사람들이 당신처럼 주석 바랍니다. 단순한. 질문에 답합니다. 훌륭한. 정말 고맙습니다! – David

+0

htmlunit 2.23'wc.getCookieManager(). addCookie (i.next());'는 다음 요청에서 쿠키를 전송하지 않았습니다. (_com.gargoylesoftware.htmlunit.Webclient_ 및 _com.gargoylesoftware의 코드를 확인하십시오. htmlunit.WebRequest_, CookieManager가 사용되지 않습니다.) 쿠키를 새로운 요청과 함께 전달하기 위해 [이 답변] (http://stackoverflow.com/a/36155793/2039709) 만 사용할 수 있습니다. 즉, 수동으로 WebRequest를 작성합니다. – user2039709

3

위의 코드는 HtmlUnit과 즉 수출에만 HtmlUnit과 agin 읽을 수있는 형식으로 (임 비판 또는 아무것도)에서만 작동합니다. 여기

하나 더 일반적인 것입니다 : (이 컬와 함께 작동)

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() + ";"); 
    } 

이제 for 루프에서 이들에 println (들)에서 문자열을 확인합니다. 그들을 텍스트 파일에 씁니다. 곱슬

작품 :

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

-b 너무 .. 컬 문서를 확인 -c 변경할 수 있습니다 ...

관련 문제