2010-05-31 47 views
1

我想用apache httpcomponents 4.0.1開發一個java http客戶端。該客戶端調用頁面「https://myHost/myPage」。 JNDIRealm使用登錄表單身份驗證在服務器上保護此頁面,所以當我嘗試獲取https://myHost/myPage時,我得到一個登錄頁面。我試圖用下面的代碼繞過它失敗:使用apache httpcomponents進行http認證

//I set my proxy 
HttpHost proxy = new HttpHost("myProxyHost", myProxyPort); 

//I add supported schemes 
SchemeRegistry supportedSchemes = new SchemeRegistry(); 
supportedSchemes.register(new Scheme("http", PlainSocketFactory 
       .getSocketFactory(), 80)); 
supportedSchemes.register(new Scheme("https", SSLSocketFactory 
       .getSocketFactory(), 443)); 

// prepare parameters 
HttpParams params = new BasicHttpParams(); 
HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1); 
HttpProtocolParams.setContentCharset(params, "UTF-8"); 
HttpProtocolParams.setUseExpectContinue(params, true); 
ClientConnectionManager ccm = new ThreadSafeClientConnManager(params, 
       supportedSchemes); 
DefaultHttpClient httpclient = new DefaultHttpClient(ccm, params); 
httpclient.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY, 
       proxy); 

//I add my authentication information 
httpclient.getCredentialsProvider().setCredentials(
new AuthScope("myHost/myPage", 443), 
new UsernamePasswordCredentials("username", "password")); 


HttpHost host = new HttpHost("myHost", 443, "https"); 
HttpGet req = new HttpGet("/myPage"); 

//show the page 
ResponseHandler<String> responseHandler = new BasicResponseHandler(); 
String rsp = httpClient.execute(host, req, responseHandler); 
System.out.println(rsp); 

當我運行此代碼時,我總是得到登錄頁面,而不是myPage。如何申請我的憑證參數以避免此登錄表單?

任何幫助將是太棒了

回答

1

HttpClient不支持表單登錄。你想要做的是基本身份驗證,它不適用於表單登錄。

您可以簡單地跟蹤登錄頁面的表單帖子併發送來自HttpClient的POST請求。

+0

謝謝!我會試試這個。 – user299957 2010-06-01 07:40:33

相關問題