![](/img/trans.png)
[英]Why do I get a WrongMethodTypeException on invokeExact even though MethodHandle is OK
[英]Why do i get 401 unauthorised even though credentials are correct?
我正在嘗試在具有基本身份驗證的 API 上發出獲取請求,但即使我有正確的用戶名和密碼,它也不起作用。
當我嘗試使用 curl(Linux 命令)訪問 API 時,它工作正常。
curl -u "user:password" -s https://dummy/link/1
我是否需要指定響應必須在enter code here
JSON?
我正在使用 Maven: org.apache.httpcomponents:httpclient:4.5.13 依賴
CredentialsProvider provider = new BasicCredentialsProvider();
UsernamePasswordCredentials credentials = new UsernamePasswordCredentials("user","password");
provider.setCredentials(AuthScope.ANY, credentials);
HttpClient client = HttpClientBuilder.create()
.setDefaultCredentialsProvider(provider)
.build();
HttpResponse response = client.execute(
new HttpGet("https://dummy/link/1");
int statusCode = response.getStatusLine()
.getStatusCode();
if(statusCode != HttpStatus.SC_OK){
System.out.println(statusCode);
}else{
System.out.println(response);
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.