Java cUrl到apachehttpclient
我通常使用cUrl在一分钟内到达终点。但我还没有到需要考虑建立客户的阶段Java cUrl到apachehttpclient,java,apache-httpclient-4.x,Java,Apache Httpclient 4.x,我通常使用cUrl在一分钟内到达终点。但我还没有到需要考虑建立客户的阶段 HttpGet getRequest = new HttpGet( "http://localhost:8080/api/private/v1/endpoint/"); HttpResponse response = httpClient.execute(getRequest); 我通常通过以下方式到达终点: curl -u "adminaccount:adminpassword" http://l
HttpGet getRequest = new HttpGet(
"http://localhost:8080/api/private/v1/endpoint/");
HttpResponse response = httpClient.execute(getRequest);
我通常通过以下方式到达终点:
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);
假设我有下面的代码
HttpGet getRequest = new HttpGet(
"http://localhost:8080/api/private/v1/endpoint/");
HttpResponse response = httpClient.execute(getRequest);
现在,我显然需要通过与之相当的考试:
HttpGet getRequest = new HttpGet(
"http://localhost:8080/api/private/v1/endpoint/");
HttpResponse response = httpClient.execute(getRequest);
-u" adminaccount:adminpassword"
不知怎么的。有什么想法吗?创建HttpClient时:
HttpGet getRequest = new HttpGet(
"http://localhost:8080/api/private/v1/endpoint/");
HttpResponse response = httpClient.execute(getRequest);
CredentialsProvider credsProvider = new BasicCredentialsProvider();
credsProvider.setCredentials(
new AuthScope("localhost", 8080),
new UsernamePasswordCredentials("adminaccount", "adminpassword"));
CloseableHttpClient httpclient = HttpClients.custom()
.setDefaultCredentialsProvider(credsProvider)
.build();
但是,是的,与其因为被告知去谷歌而大发雷霆,不如去谷歌一下。例如,从谷歌搜索“HttpClient身份验证示例”开始。该网站的规则非常明确——“先做你的家庭作业”如果你要否决投票,至少要留下评论。看看HttpClient文档:用谷歌搜索一下什么是HTTP授权头,如何在HttpGet中设置它?--提示:在你的情况下,它是关于基本的身份验证模式…喜欢这种“为什么不谷歌”的评论-当我明显处于一个我甚至不知道谷歌搜索什么的位置时。你以前从来没有遇到过一个完全陌生的问题吗?天哪,你都很好,而且有毒,你几乎会认为我在ruby on rails网站上问过这个问题。我确实用谷歌搜索过这个问题,但它与curl-u不一样,因为curl-u工作,这个方法返回的http响应为406。
HttpGet getRequest = new HttpGet(
"http://localhost:8080/api/private/v1/endpoint/");
HttpResponse response = httpClient.execute(getRequest);