Apache HttpClient(4.1及更高版本):如何进行基本身份验证? [英] Apache HttpClient (4.1 and newer): how to do basic authentication?

查看:103
本文介绍了Apache HttpClient(4.1及更高版本):如何进行基本身份验证?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何为httpClient库的默认客户端添加基本身份验证?我已经看到了使用client.getCredentialProvider()的示例,但是我认为所有这些方法都适用于库版本4.0.1或3.x.是否有一个新的示例如何执行此操作?非常感谢.

How do I add basic authentication for the default client of the httpClient library? I have seen examples where they use client.getCredentialProvider(), however I think all of this methods are for library version 4.0.1 or 3.x. Is there a new example of how to do this? Thanks a lot.

推荐答案

我们使用HttpClient进行基本身份验证,但不使用CredentialProvider.这是代码:

We do basic authentication with HttpClient, but we do not use CredentialProvider. Here's the code:

HttpClient client = factory.getHttpClient(); //or any method to get a client instance
Credentials credentials = new UsernamePasswordCredentials(username, password);
client.getState().setCredentials(AuthScope.ANY, credentials);

更新: 注释中指出,HttpClient.getState()方法在以下版本的版本3.x中可用API .但是, API的较新版本不支持该方法.

UPDATE: A stated in the comments, the HttpClient.getState() methos is available in version 3.x of the API. However, newer versions of the API doesn't support that method.

这篇关于Apache HttpClient(4.1及更高版本):如何进行基本身份验证?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆