2012-09-07 149 views
2

我有一個基於Apache HTTP客戶端的HTTP客戶端,它似乎沒有SSL證書的問題。我有一個針對全球認可的證書和自簽名證書的自定義SSLSocketFactory的單元測試。通過代理與HTTP客戶端HTTPs

但是,當我在代理後面運行相同的代碼時,它停止工作。我不斷收到這種可怕的例外:

javax.net.ssl.SSLPeerUnverifiedException: peer not authenticated 
at com.sun.net.ssl.internal.ssl.SSLSessionImpl.getPeerCertificates(SSLSessionImpl.java:352) 
at org.apache.http.conn.ssl.AbstractVerifier.verify(AbstractVerifier.java:128) 
at org.apache.http.conn.ssl.SSLSocketFactory.connectSocket(SSLSocketFactory.java:572) 
at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:180) 
at org.apache.http.impl.conn.ManagedClientConnectionImpl.open(ManagedClientConnectionImpl.java:294) 
at org.apache.http.impl.client.DefaultRequestDirector.tryConnect(DefaultRequestDirector.java:640) 
at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:479) 
at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:906) 
at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:805) 

我的代碼減少到最低限度,它仍然拋出同樣的異常。代碼:

URI uri = new URI("https://www.google.com"); 
    DefaultHttpClient client = new DefaultHttpClient(); 
    client.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY, 
      new HttpHost("proxy.int", 8080, "https")); 

    HttpUriRequest request = new HttpGet(uri); 
    HttpResponse response = client.execute(request); 

我不知道,如果它使用默認的SSL設置,如果未指定任何所以我加了它明確以及:

URI uri = new URI("https://www.google.com"); 
    DefaultHttpClient client = new DefaultHttpClient(); 
    client.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY, 
      new HttpHost("proxy.int", 8080, "https")); 

    client.getConnectionManager().getSchemeRegistry().register(
      new Scheme("https", 443, SSLSocketFactory.getSystemSocketFactory())); 

    HttpUriRequest request = new HttpGet(uri); 
    HttpResponse response = client.execute(request); 

我也試過在getSocketFactory()(未完全確定它與getSystemSocketFactory()有什麼不同,但仍然是相同的錯誤。

編輯

代理具有可選的認證,我已經既沒有試過。驗證信息使用以下代碼設置:

client.getCredentialsProvider().setCredentials(
     new AuthScope("proxy.int", 8080), 
     new UsernamePasswordCredentials("user", "password") 
    ); 

完全相同的錯誤。

+0

是否代理服務器需要身份驗證? – Santosh

+0

它是可選的身份驗證,我嘗試過和沒有(更多細節添加到問題) – nablex

回答

6

的問題是在代理報關,我必須指定「HTTP」,而不是「https」開頭:

client.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY, 
     new HttpHost("proxy.int", 8080, "http"));