httpclient进行basic auth认证

时间:2021-04-07 16:19:55
private HttpClientContext context = HttpClientContext.create();

public void addUserOAuth(String username,String password){

        CredentialsProvider  credsProvider = new BasicCredentialsProvider();
        org.apache.http.auth.Credentials credentials = new org.apache.http.auth.UsernamePasswordCredentials(username,password);
        credsProvider.setCredentials(org.apache.http.auth.AuthScope.ANY,credentials);
        this.context.setCredentialsProvider(credsProvider);
    }

 

然后在调用httpclient.post或者get方法前,调用addUserOAuth方法