2011-05-17 113 views
0

我發佈之前,但現在我似乎真的看到了問題,只是無法修復它。Apache HttpClient沒有收到所有的cookies?

我試圖登錄到我小學三年級的網站,甚至使應用程序爲它以後,當我使用Chrome檢查所創建的餅乾,我得到這一切,

Cookie:appName=chippewa_falls; tool=""; selection=""; districtID=1; endYear=2011; calendarID=0; permCalendarID=0; JSESSIONID=BE5AEF51EAA72975150FC2D0F77DDE13 

但是,當我的程序將打印所有收到的餅乾,我只得到這個

  • [version: 0][name: JSESSIONID][value: BC1BAA33BEB23DC27B7883AC24934A1D][domain: campus.chipfalls.k12.wi.us][path: /campus][expiry: null]

這裏是我的代碼,

public static void main(String[] args) throws Exception { 


    DefaultHttpClient httpclient = new DefaultHttpClient(); 
    HttpGet httpget = new HttpGet("https://campus.chipfalls.k12.wi.us/campus/portal/chippewa_falls.jsp"); 
    httpget.addHeader("Referer", "http://cfsd.chipfalls.k12.wi.us//high/"); 
    HttpResponse response = httpclient.execute(httpget); 
    HttpEntity entity = response.getEntity(); 

    System.out.println("Login form get: " + response.getStatusLine()); 
    if (entity != null) { 
     InputStream input = entity.getContent(); 
     BufferedReader reader = new BufferedReader(new InputStreamReader(input)); 
     String ln = ""; 
     while((ln = reader.readLine()) != null) { 
      System.out.println("During Get - " + ln); 
     } 
    } 
    System.out.println("Initial set of cookies:"); 
    List<Cookie> cookies = httpclient.getCookieStore().getCookies(); 

    if (cookies.isEmpty()) { 
     System.out.println("None"); 
    } else { 
     for (int i = 0; i < cookies.size(); i++) { 
      System.out.println("- " + cookies.get(i).toString()); 
     } 
    } 

注意,僅僅是它的GET部分,

感謝所有幫助:)

編輯:我沒有忘記提及,它使用HTTPS,但說實話,我不知道這與apache客戶端是否有關係。

回答

0

這些cookies必須在您登錄後才能設置,而不是之前。尋找與你有網址的wget輸出上面,你看到這一點:

Resolving campus.chipfalls.k12.wi.us... 205.213.253.11 
Connecting to campus.chipfalls.k12.wi.us|205.213.253.11|:443... connected. 
WARNING: Certificate verification error for campus.chipfalls.k12.wi.us: self signed certificate in certificate chain 
HTTP request sent, awaiting response... 
    HTTP/1.1 200 OK 
    Server: Apache-Coyote/1.1 
    Set-Cookie: JSESSIONID=004549BDBCBDFB8289EBF859A4E743B2; Path=/campus; Secure 
    Content-Type: text/html;charset=utf-8 
    Content-Length: 6352 
    Date: Tue, 17 May 2011 06:00:38 GMT 
    Connection: keep-alive 
Length: 6,352 (6.2K) [text/html] 

這是你看到同樣的事情。在您實際登錄之前,您不會看到其他Cookie。

+0

謝謝:)這就是我在想什麼,所以我在發佈之前大約一個小時就修好了它,不過謝謝。現在我遇到了一個新問題,頁面使用JavaScript將所有內容打印出來,無論如何我仍然可以獲得輸出結果嗎?如果必須,我會發佈一個新問題。 – Austin 2011-05-18 13:38:09

+0

嗯,爲此你可能會遇到一些困難:你可能想看看AJAX調用頁面所做的事情,然後模擬這些內容,但是隻要讓佈局正確完成,那很麻煩。有幾個Java瀏覽器模擬器庫(我認爲Selenium可以讓你在Java代碼中使用他們的瀏覽器模擬器),所以你可能需要走這條路線。 – Femi 2011-05-18 13:41:17