2011-07-14 41 views
3

我希望能夠使用java代碼使用我的用戶名/密碼登錄到HTTPS網站(如Gmail)。 我試過使用我自己的代碼和HTTPClient包(都允許我登錄到正常的網站(如Facebook)),但不能進入HTTPS網站。 有人能告訴我什麼是必要使用Java登錄到https網站? 謝謝!如何使用Java登錄到HTTPS網站?

示例代碼:

import org.apache.http.*; 

import org.apache.http.auth.*; 

import org.apache.http.client.methods.*; 

import org.apache.http.impl.client.*; 

import org.apache.http.util.*; 



public class ClientAuthentication { 

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

    DefaultHttpClient httpclient = new DefaultHttpClient(); 

    try { 

     httpclient.getCredentialsProvider().setCredentials(new AuthScope(AuthScope.ANY_HOST, AuthScope.ANY_PORT), new UsernamePasswordCredentials("[email protected]", "password")); 
     HttpGet httpget = new HttpGet("https://www.google.com/accounts/ServiceLogin?service=mail&passive=true&rm=false&continue=https%3A%2F%2Fmail.google.com%2Fmail%2F%3Fui%3Dhtml%26zy%3Dl&bsv=llya694le36z&ss=1&scc=1&ltmpl=default&ltmplcache=2&hl=en&from=logout"); 

     HttpResponse response = httpclient.execute(httpget); 
     HttpEntity entity = response.getEntity(); 

     System.out.println("----------------------------------------"); 
     System.out.println(response.getStatusLine()); 

     if (entity != null) { 

      System.out.println("Response content length: " + entity.getContentLength()); 

      entity.writeTo(System.out); 
     } 

     EntityUtils.consume(entity); 

    } finally { 

     httpclient.getConnectionManager().shutdown(); 
    } 
} 
} 
+1

憑證用於諸如BASIC,集成瀏覽器驗證(大多數瀏覽器中出現的本機彈出窗口)之類的內容。大多數HTTPS站點使用基於表單的身份驗證,這意味着您需要POST數據,然後使用該cookie。 –

+0

假設您運行的是適度更新的Java版本,HTTPClient可以正常使用SSL。 – Ben

回答

0

登錄到一個HTTPS網站,你需要檢查你的系統時間,因爲證書是2個固定日期

0

你說的是認證或連接https服務器之間的有效(沒有客戶端身份驗證)? 許多網站(如谷歌,臉譜)使用OAuth或OpenID - 瞭解它。 如果它的其他東西(如你使用簡單的POST)。問題是什麼?你是否獲得堆棧跟蹤?