java - Java Apache HTTP 401 响应返回正确的凭据
问题描述
我正在尝试使用 Apache HttpClient 访问 REST API 链接,但一直返回 401 错误。在提示输入密码后,当我在浏览器中转到 URL 时,我可以登录。我正在使用的代码如下:
CredentialsProvider provider = new BasicCredentialsProvider();
UsernamePasswordCredentials credentials = new UsernamePasswordCredentials(creds.get(0), creds.get(1));
provider.setCredentials(AuthScope.ANY, credentials);
AuthCache authCache = new BasicAuthCache();
authCache.put(new HttpHost(uri.getHost(), uri.getPort(), "https"), new BasicScheme());
BasicHttpContext context = new BasicHttpContext();
context.setAttribute(ClientContext.CREDS_PROVIDER, provider);
context.setAttribute(ClientContext.AUTH_CACHE, authCache);
DefaultHttpClient client = new DefaultHttpClient();
client.setHttpRequestRetryHandler(new DefaultHttpRequestRetryHandler());
client.setCredentialsProvider(provider);
HttpResponse response = null;
try
{
// response = client.execute(new HttpGet(uri));
response = client.execute(new HttpGet(uri), context);
}
catch(IOException e)
{
logger.error("Error running authenticated get request: " + e);
}
我正在使用 HttpClient 4.2.3,不幸的是我无法升级它。
任何帮助,将不胜感激!谢谢!
编辑:原来我需要提供证书,比如在 curl 中使用 -cacert,但是我找不到这样的例子!
解决方案
由于您需要提供证书,因此这可能会有所帮助:
我认为该示例符合 4.2.3 。
推荐阅读
- ibm-mq - IBM MQ - 我无法使用通配符语法订阅主题
- google-sheets - 动态添加行以展开 QUERY
- json - 如何使用 jolt 规范更改 mongodb 输出?
- php - Elastich 搜索 - ICU 排序关键字字段 - 挪威语 - 排序时将弹性搜索视为 å
- amazon-web-services - 计划在与 azure 比较的 aws 中启动和停止 Ec2 实例?
- collections - postman 集合的特定文件夹从工具成功运行,但在使用 --folder 选项从 newman 工具运行时抛出 401 错误
- python - 刮板不从动态网页返回结果
- python-3.x - 我是初学者,对此有问题!我希望输出仅在 mainList 中的单独子列表中打印一次
- javafx - javafx中来自url的实时视频流
- function - 不和谐机器人编码函数调用