2010-09-28 18 views
1

我正在嘗試使用Apache HTTPClient 4.x使用example from the site進行基本認證,唯一的變化是我已經將一些細節提取到常量中,但是我沒有我期待的結果。從Apache HTTPClient Basic獲取意外401認證

即是說,隨着日誌打開調試,我得到:「DEBUG主客戶端.DefaultHttpClient:1171 - 憑證找不到」,然後從服務器401錯誤。

我已經手動驗證了我配置的憑據是否正確,並且「未找到憑證」消息使我相信證書從未在請求​​中傳遞。

關於我可能會做錯什麼想法?

DefaultHttpClient httpClient = new DefaultHttpClient(); 

    httpClient.getCredentialsProvider().setCredentials(
      new AuthScope(API_HOST, API_PORT), 
      new UsernamePasswordCredentials(API_USERNAME, API_PASSWORD)); 

    HttpGet httpget = new HttpGet(API_TEST_URL); 

    System.out.println("executing request" + httpget.getRequestLine()); 
    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()); 
    } 
    if (entity != null) { 
     entity.consumeContent(); 
    } 

    httpClient.getConnectionManager().shutdown(); 

回答

4

您確定AuthScope設置正確嗎?嘗試設置它像這樣只是爲了看看問題是否有

new AuthScope(AuthScope.ANY_HOST, AuthScope.ANY_PORT) 
+0

奇...擔任你的變化預期,因此它必須是AuthScope,儘管他們似乎是正確的。哦,我現在至少在正確的軌道上。謝謝! – Thody 2010-09-28 14:06:21