Apache HttpClient (4.1 and newer): how to do basic authentication?

前端 未结 6 1878
予麋鹿
予麋鹿 2021-01-30 16:33

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 th

6条回答
  •  北海茫月
    2021-01-30 17:21

    CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
    credentialsProvider.setCredentials(AuthScope.ANY, 
        new UsernamePasswordCredentials("username", "password"));
    CloseableHttpClient httpClient = 
        HttpClientBuilder.create().setDefaultCredentialsProvider(credentialsProvider).build();
    

提交回复
热议问题