cUrl 到 apache HttpClient [英] cUrl to apache HttpClient
问题描述
我通常使用 cUrl 一分钟到达我的终点.但我还没有到需要考虑构建客户端的阶段.
I normally use cUrl to hit my endpoint at the minute. But am not at the stage where I need to think about building the client.
我通常通过以下方式到达我的端点:
I normally hit my endpoint the following way :
curl -u "adminaccount:adminpassword" http://localhost:8080/api/private/v1/endpoint/
假设我有以下代码
HttpGet getRequest = new HttpGet(
"http://localhost:8080/api/private/v1/endpoint/");
HttpResponse response = httpClient.execute(getRequest);
现在,我显然需要传入等效的内容:
Now, I obviously need to pass in the equivalent of that :
-u" adminaccount:adminpassword"
不知何故.有什么想法吗?
somehow. Any ideas?
推荐答案
创建 HttpClient 时:
When creating your HttpClient:
CredentialsProvider credsProvider = new BasicCredentialsProvider();
credsProvider.setCredentials(
new AuthScope("localhost", 8080),
new UsernamePasswordCredentials("adminaccount", "adminpassword"));
CloseableHttpClient httpclient = HttpClients.custom()
.setDefaultCredentialsProvider(credsProvider)
.build();
但是是的,不要因为被告知用谷歌搜索而生气,而是去谷歌搜索.例如,从谷歌搜索HttpClient 身份验证示例"开始.网站的规则很明确——先做功课"
But yeah instead of getting in a huff about being told to google it, just go google it. Start with googling for "HttpClient authentication example" for example. The rules of the site are quite clear - "Do your homework first"
这篇关于cUrl 到 apache HttpClient的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!