我有一個HttpClient
實例,它由多個線程共享。我想用它來做一個單一的認證請求。由於只有單個請求需要進行身份驗證,因此我不想按照documentation中所述修改HttpClient
實例。這是我所做的,而不是工作。據我所知,它看起來並不像CredentialsProvider
正在使用。有小費嗎?使用httpclient 4.x驗證單個請求
HttpContext context = null;
if(feedSpec.isAuthenticated()) {
context = new BasicHttpContext();
CredentialsProvider credsProvider = new BasicCredentialsProvider();
credsProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(feedSpec.getHttpUsername(), feedSpec.getHttpPassword()));
context.setAttribute(ClientContext.CREDS_PROVIDER, credsProvider);
context.setAttribute(ClientPNames.HANDLE_AUTHENTICATION, true);
}
HttpGet httpGet = new HttpGet(feedSpec.getUri());
HttpResponse httpResponse = httpClient.execute(httpGet, context);
您使用的是什麼樣的身份驗證? BASIC,DIGEST,FORM? – rodrigoap 2010-03-25 14:43:29
基本認證 – 2010-03-25 14:57:40
HttpClient 4是一個糟糕的混亂,國際海事組織。它承諾了很多,但對於非常簡單的情況來說這很難使用。我堅持使用v3。 – skaffman 2010-03-26 11:48:36